Is there a way to declare a successive string literal union in Typescript
I want to declare a string literal union type: "10" | "11" | ... "99".
I came up with a workaround, but didn't work
const arr = Array.from({
length: 90
}, (_, i) => i + 10) as const; // expected arr type is readonly [10, 11, ..., 99]
// If above works, then
type NumberToString<T extends number> = `${T}`;
type Successive = NumberToString<typeof arr[number]>;
I wonder if there is a way to quickly define "10" to "99" on top of type level?
This should be possible in next (4.5) version of TypeScript where type instantiation depth limit should be increased
type _NextValue<From, LastValue, R extends any[]> = LastValue extends number
? R['length']
: R['length'] extends From ? From : null
/**
* Fill array with `null` placeholders until index `From`
* Then append array length as a member until getting to `To` length
* Example: `_Values<5, 11>` will result in `[null, null, null, null, null, 5, 6, 7, 8, 9, 10]`
*/
type _Values<From, To, R extends any[] = [], LastValue = null> = R['length'] extends To ? R :
_Values<
From,
To,
[...R, _NextValue<From, LastValue, R>],
_NextValue<From, LastValue, R>
>
/**
* Exclude nulls and convert to strings
*/
type NumbersToStrings<T extends Array<number | null>> = `${NonNullable<T[number]>}`;
type N10_99 = NumbersToStrings<_Values<10, 100>> // "10" | "11" | "12" | "13" | "14" | "15" | ... | "99"
Playground