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

ParseInteger and ParseBoolean #449

Open
sindresorhus opened this issue Aug 27, 2022 · 1 comment
Open

ParseInteger and ParseBoolean #449

sindresorhus opened this issue Aug 27, 2022 · 1 comment

Comments

@sindresorhus
Copy link
Owner

sindresorhus commented Aug 27, 2022

Would it be useful to be able to convert a number in a string to a number literal type? '100' => 100.

I guess a more strictly typed Number.parseInt could take advantage of this.

type SomeNum = "100" extends `${infer U extends number}` ? U : never;
// 100

We cannot do ParseNumber because if it doesn't match exactly, it just returns a number type instead of 100:

type SomeNum = "100.0" extends `${infer U extends number}` ? U : never;
// number

We could also maybe do ParseBoolean and maybe Is methods too (IsInteger)?

Maybe it's not that useful. Feedback wanted.

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • The funding will be given to active contributors.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@anuraghazra
Copy link

Hey landed here while I was exploring the 48094 PR.
I think a type level string to number would be handy in some situations.

We cannot do ParseNumber because if it doesn't match exactly, it just returns a number type instead of 100:

Yes, that's a bummer. But actually I found a workaround.

https://tsplay.dev/mbn8Pm

Here's the tweet where I was geeking about this today

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants