-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Feature request: Ability to parse a stringified number on type-level #47141
Comments
Looks like a duplicate of #26382. I would love to have built-in support for It's an... interesting approach to say that the motivating example for |
Hahaha yeah I was too honest with the motivation part, I just innocently wrote what motivated me instead of writing a more saner motivation that would perhaps motivate the typescript team :P and very apt anology btw xD But yeah as you mentioned perhaps another motivation is to make the declare let x: [string, number]
let foo: string = x[Number("0")]
// ~~~
// Error: `string | number` is not assignable to `string` With Although I would not call this a duplicate of #26382 because it's not, but not even a spiritual duplicate because this feature would only enable arithmetic of number literals, whereas the linked feature (if implemented fully instead of just literals) would have to handle generics too. Which would basically make typescript a dependently typed language, you'll be able to prove things with it... declare const lhs =
<A extends number, B extends number>
(a: A, b: B) => (A + B) * (A + B)
declare const rhs =
<A extends number, B extends number>
(a: A, b: B) => A * A + A * B + B * A + B * B
const prove =
<A extends number, B extends number>
(a: A, b: B) => {
let x = lhs(a, b)
let y: typeof x = rhs(a, b)
// If this compiles, which it would, lhs and rhs are equal
} So having things like Regarding a |
I didn't mean to bikeshed the naming; I was using |
Ah gotcha, even I didn't mean to bikeshed on the name, I thought you were suggesting that instead of resolving |
You can write interface StringToNumber {
"0": 0,
"1": 1,
"2": 2,
"3": 3, // etc
} if the numbers involved are too high for this approach to be reasonable, you are doing math that is not appropriate to be done in the type system. |
The math part was just an example, what if one wants to make this compile... const toNumber = <N extends number>(s: StringifiedNumber<N>) => Number(s) as N
type StringifiedNumber<N extends number> = `${N}`
const x: 1000 = toNumber("1000") A type system that allows writing parsers, that added tail call optimization so that users can operate on larger strings and many more things, doesn't want to allow a simple operation of going from You can always state that "We're adding this feature but it's not meant from doing typelevel maths with large numbers" and then people who still would do it (like in case of the rest of typescript features :P) would be on their own. |
For context, this never works. |
Haha yeah I know hence the "like in case of the rest of typescript features :P" But what I meant was by stating that, the risk of "misusing" it is transferred to them. I'd do the same if I were to publish a library that does typelevel maths, I'd say "TypeScript team has said not to do this with large numbers" and then the risk further gets transferred to the person who actually would be responsible. In this way one is not deciding for someone else how much risk they should take, as different people have different amounts of courage to take risks. But at the same time I understand not everyone wants to operate from this model, and that's okay. Eitherway, I'm happy with typescript :P |
We can have this trick to parse string to number from 0 to 999 since TS 4.5 has tail recursion optimization type ToNumber<T extends string, R extends any[] = []> =
T extends `${R['length']}` ? R['length'] : ToNumber<T, [1, ...R]>; |
Ah that's nice especially the fact it can go upto 999 |
Yeah, this PR may help |
|
Yeah it's essentially a duplicate as both issues are asking for the same feature |
Suggestion
π Search Terms
parse a number from stringified number on type-level, infer a number literal from a template string literal, template string literal to number literal
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
Currently there is no way to parse
"100"
into100
on type-level. One could try something like...But
T0
is"100"
, rightfully so. And inferring100
instead would be a breaking change. Let's try this...Here
T1
isnumber
when it could have been inferred as100
. The feature request is to makeT1
100
, this too would be a breaking change but I think it'd break things a lot less. I think it makes sense to infer the narrowest possible type when we can.π Motivating Example
Type-level arithmetic used to be impossible with large number prior to template literal types. But now we can stringify numbers on type-level then split them into digits and then do a carry-save addition. This makes type-level arithmetic very fast and "doable". Example adding
12345
&6789
...But then there's no way to parse the string back to a number.
Type-level arithmetic makes working with dependent-ish types more complete. It'd be great we can have this feature, thanks!
The text was updated successfully, but these errors were encountered: