type-fest icon indicating copy to clipboard operation
type-fest copied to clipboard

ParseInteger and ParseBoolean

Open sindresorhus opened this issue 1 year ago • 0 comments

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
  • https://devblogs.microsoft.com/typescript/announcing-typescript-4-8/#improved-inference-for-infer-types-in-template-string-types
  • https://github.com/microsoft/TypeScript/pull/48094

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.

sindresorhus avatar Aug 27 '22 10:08 sindresorhus