Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript-aware helper functions #372

Open
IlyaSemenov opened this issue Jun 6, 2023 · 1 comment
Open

Typescript-aware helper functions #372

IlyaSemenov opened this issue Jun 6, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@IlyaSemenov
Copy link
Contributor

IlyaSemenov commented Jun 6, 2023

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

@IlyaSemenov IlyaSemenov added the enhancement New feature or request label Jun 6, 2023
@Beraliv
Copy link
Collaborator

Beraliv commented Jun 15, 2023

Hey @IlyaSemenov!

Thank you for your suggestion!

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:

  1. ts-reset already does it by overriding filter with BooleanConstructor
  2. 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants