Description
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 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)
const listOfOptionalStrings: Array<string|undefined> = []
const listOfStrings = 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:
const isBoolean = Boolean as any as <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
const list = ["foo", "bar"] as const
const item: 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:
function includes<T extends U, U>(list: ReadonlyArray<T>, el: U): el is T {
return list.includes(el as T)
}
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