You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Disclaimer: I am sorry for misusing this form. if this repo had discussions enabled, I sure wouldn't open this as an issue.
I was wondering if ts-essentials is a reasonable place for more one-line generic runtime helpers alongside assert which repeat built-in node functions with better type safety?
Examples
To be concrete, I have two cases in mind:
1. filter(Boolean)
constlistOfOptionalStrings: Array<string|undefined>=[]constlistOfStrings=listOfOptionalStrings.filter(Boolean)// Expected: listOfStrings is typed as string[]// Actual: listOfStrings if still typed as (string | undefined)[]
This can be handled with one-line helper:
constisBoolean=Booleanasanyas<T>(x: T|false|undefined|null|""|0)=>x is T;
I know that e.g. ramda exports isNotNil which arguably makes more sense, but the proposed isBoolean will supposedly tree-shake into nothing so it kinda fits as a typings-only helper.
2. Array.includes
constlist=["foo","bar"]asconstconstitem: string="bar"if(list.includes(item)){// Expected: no ts errors, item here is typed as "foo" | "bar"// Actual: list.includes emits ts error, item is still typed as string}
This can be handled with one-line helper:
functionincludes<TextendsU,U>(list: ReadonlyArray<T>,el: U): el is T{returnlist.includes(elasT)}
This will not tree-shake into nothing, but neither lodash nor ramda publishes this with two generic params. I agree that it probably makes more sense to pursue better typings there, but still I wanted to ask if you see ts-essentials a place for stuff like that, and where's the boundary if not.
Additional Info
No response
The text was updated successfully, but these errors were encountered:
Let me think about it, they make sense to me but I need to check previous issues to kind of see the decisions that have been made in the past to align my thoughts with them and understand if it fits or not
What can help you right now:
ts-reset already does it by overriding filter with BooleanConstructor
ts-reset does it as well for includes with readonly arrays
I think it's possible to add it to ts-essentials as runtime Utility functions but let me have a closer look and I will come back to you
Feel free to 🏓 ping ⏰ me if it takes longer for me to respond, apologies for that in advance
What
Disclaimer: I am sorry for misusing this form. if this repo had discussions enabled, I sure wouldn't open this as an issue.
I was wondering if
ts-essentials
is a reasonable place for more one-line generic runtime helpers alongsideassert
which repeat built-in node functions with better type safety?Examples
To be concrete, I have two cases in mind:
1. filter(Boolean)
This can be handled with one-line helper:
I know that e.g.
ramda
exportsisNotNil
which arguably makes more sense, but the proposedisBoolean
will supposedly tree-shake into nothing so it kinda fits as a typings-only helper.2. Array.includes
This can be handled with one-line helper:
This will not tree-shake into nothing, but neither
lodash
norramda
publishes this with two generic params. I agree that it probably makes more sense to pursue better typings there, but still I wanted to ask if you see ts-essentials a place for stuff like that, and where's the boundary if not.Additional Info
No response
The text was updated successfully, but these errors were encountered: