- TypeScript 4.1 版本新功能
- 提供了连接字符串字面量的能力
- 可以把非字符串基本类型的字面量转换为对应的字符串字面量类型
type Direction = 'left' | 'right' | 'top' | 'bottom';
type CssPadding = `padding-${Direction}`;
type MarginPadding = `margin-${Direction}`;
type EventName<T extends string> = `${T}Changed`;
type Concat<S1 extends string, S2 extends string> = `${S1}-${S2}`;
type ToString<T extends string | number | boolean | bigint> = `${T}`;
type T0 = EventName<'foo'>;
type T1 = Concat<'Hello', 'World'>;
type T2 = ToString<'阿宝哥' | 666 | true | -1234n>;
- 当类型占位符的实际类型是联合类型(A | B | C)的话,就会被自动展开
type T3 = EventName<'foo' | 'bar' | 'baz'>;
type T4 = Concat<'top' | 'bottom', 'left' | 'right'>;