[英]Template Literal Types Typescript repeat
是否可以使用模板文字类型构建重复? 例如:
type Hex = 'a' | 'b' | 'c'| ...;
type Id = `${Hex}${Hex}...` // Here I want to say Id is N hex long.
原则上,这可以通过 TS 4.1 中的递归条件类型实现:
type Decrement = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
type RepeatString<S extends string, N extends number> =
N extends 1 ? S :
`${S}${RepeatString<S, Decrement[N]>}`
type T11 = Decrement[5] // 4
type T12 = RepeatString<"foo", 3> // "foofoofoo"
type T13 = RepeatString<Hex, 3> // "aaa" | "aab" | ... | "ccb" | "ccc"
请注意以下限制:
请注意,联合类型的叉积分布可能会迅速升级为非常大且成本高昂的类型。 另请注意,联合类型限制为少于 100,000 个成分[.](我的重点)
例如, Hex2
是'a' | 'b' |... | 'i' | 'j'
'a' | 'b' |... | 'i' | 'j'
'a' | 'b' |... | 'i' | 'j'
(10 个联合部分), type T14 = RepeatString<Hex2, 5>
将触发以下错误( 10*10*10*10*10
排列):
表达式产生的联合类型太复杂而无法表示。(2590)
在这种情况下,需要减少联合部分的数量,例如查找更大的字符串块而不是原子字符。
type RepeatStringAlt<S extends string, N extends number> = RepeatStringAltRec<S, TupleOf<unknown, N>>
type RepeatStringAltRec<S extends string, T extends unknown[]> =
T["length"] extends 1 ? S : `${S}${RepeatStringAltRec<S, DropFirst<T>>}`
type TupleOf<T, N extends number> = N extends N ? number extends N ? T[] : _TupleOf<T, N, []> : never;
type _TupleOf<T, N extends number, R extends unknown[]> = R['length'] extends N ? R : _TupleOf<T, N, [T, ...R]>;
type DropFirst<T extends readonly unknown[]> = T extends readonly [any?, ...infer U] ? U : [...T];
type T23 = RepeatStringAlt<"foo", 3> // "foofoofoo"
这摆脱了使用Decrement
手动计算数字范围。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.