If you want to achieve something similar, you can follow this approach:
type Key = `data-element-${1|2|3|4|5|6|7|8|9|0}`
const obj:Record<Key, string> = {
'data-element-0': 'something',
'data-element-1': 'something else',
'data-element-2': 'something as well',
'data-element-3': 'something to feel included',
'data-element-yu': 'something to feel included', // error
};
UPDATE
I have also created helpers for double numbers from 0-99:
type NonZeroDigit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
type NumberHelper = {
[P in NonZeroDigit]: {
[Z in NonZeroDigit]: `${P}${Z}`
}
}
type NestedValues<T extends Record<string, Record<string, string>>> = {
[P in keyof T]: P extends string ? Values<T[P]> : never
}
type Values<T> = T[keyof T]
type RemoveTrailingZero<T extends string> = T extends `${infer Fst}${infer Snd}` ? Fst extends `0` ? `${Snd}` : `${Fst}${Snd}` : never;
type Numbers_99 = RemoveTrailingZero<Values<NestedValues<NumberHelper>>>
UPDATE
Here's a utility function for generating number ranges from 0 to 99999:
type Values<T> = T[keyof T]
type LiteralDigits = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
type NumberString<T extends number> = `${T}`
type AppendDigit<T extends number | string> = `${T}${LiteralDigits}`
type MakeSet<T extends number> = {
[P in T]: AppendDigit<P>
}
type RemoveTrailingZero<T extends string> = T extends `${infer Fst}${infer Rest}` ? Fst extends `0` ? RemoveTrailingZero<`${Rest}`> : `${Fst}${Rest}` : never;
type From_1_to_999 = RemoveTrailingZero<Values<{
[P in Values<MakeSet<LiteralDigits>>]: AppendDigit<P>
}>>
type By<V extends NumberString<number>> = RemoveTrailingZero<Values<{
[P in V]: AppendDigit<P>
}>>
type From_1_to_99999 =
| From_1_to_999
| By<From_1_to_999>
| By<From_1_to_999
| By<From_1_to_999>>
Demo
UPDATE 3
If you prefer generating literal numbers instead of strings, you can use the following code snippet borrowed from here:
type PrependNextNum<A extends Array<unknown>> = A['length'] extends infer T ? ((t: T, ...a: A) => void) extends ((...x: infer X) => void) ? X : never : never;
type EnumerateInternal<A extends Array<unknown>, N extends number> = { 0: A, 1: EnumerateInternal<PrependNextNum<A>, N> }[N extends A['length'] ? 0 : 1];
type Enumerate<N extends number> = EnumerateInternal<[], N> extends (infer E)[] ? E : never;
type Result = Enumerate<43> // 0 | 1 | 2 | ... | 42
*UPDATE 8 September 2021
With TS 4.5, utilizing the Tail recursion PR, longer number ranges can be generated.
Here is an example:
type MAXIMUM_ALLOWED_BOUNDARY = 999
type ComputeRange<
N extends number,
Result extends Array<unknown> = [],
> =
(Result['length'] extends N
? Result
: ComputeRange<N, [...Result, Result['length']]>
)
const ComputeRange = (N: number, Result: number[] = []): number[] => {
if (Result.length === N) {
return Result
}
return ComputeRange(N, [...Result, Result.length])
}
// 0 , 1, 2 ... 998
type NumberRange = ComputeRange<MAXIMUM_ALLOWED_BOUNDARY>[number]
Related question
Playground