条件类型就是“类型层的 if/else”:根据类型是否满足某个条件,返回不同的类型。
type IsString<T> = T extends string ? true : false
type A = IsString<'hi'> // true
type B = IsString<123> // false
常见用途:从复杂类型里“提取/转换”出你想要的类型,比如从 Promise<T> 里拿到 T:
type UnwrapPromise<T> = T extends Promise<infer R> ? R : T
type X = UnwrapPromise<Promise<number>> // number