Created
July 12, 2022 06:24
-
-
Save vezaynk/310b14006af4531bb827e84111a3db15 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface StringDigitToNumber { | |
'0': 0, | |
'1': 1, | |
'2': 2, | |
'3': 3, | |
'4': 4, | |
'5': 5, | |
'6': 6, | |
'7': 7, | |
'8': 8, | |
'9': 9, | |
} | |
type Digit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | '0'; | |
type NumericStringAsDigitArray<T extends `${Digit}${string}`> = | |
T extends `${infer L extends Digit}${infer F extends string}` ? | |
F extends `${Digit}${string}` ? [StringDigitToNumber[L], ...NumericStringAsDigitArray<F>] : [StringDigitToNumber[L]] : never; | |
type Flatten<T extends any[]> = | |
T extends [infer First, ...infer Rest] | |
? First extends any[] | |
? Flatten<[...First, ...Rest]> | |
: [First, ...Flatten<Rest>] | |
: [] | |
type OneTwoThree = NumericStringAsDigitArray<"123">; | |
// ^? type OneTwoThree = [1, 2, 3] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Going to bed.
Using others' solutions, we can join and convert the result to a number. A function
MinusOne<>
generic is waiting: