// 套路二: 模式匹配做提取
// 提取 value 的类型
type GetValue<P> = P extends Promise<infer value> ? value : never
type GetValueRes = GetValue<Promise<'vvv'>>
// 删除数组第一个
type ShiftArr<T extends unknown[]> = T extends [] ? [] : T extends [infer Fistr, ...infer Rest] ? Rest : never
type ShiftArrRes = ShiftArr<[1, 2, 3]>
// 判断字符串是否以某个前缀开头
type StartsWith<Str extends string, perfix extends string> = Str extends `${perfix}${string}` ? true : false
type StartsWithType = StartsWith<'vvvcode', 'vvv'>
// 字符串替换
type ReplaceStr<Str extends string, From extends string, To extends string> =
Str extends `${infer perfix}${From}${infer suffix}` ? `${perfix}${To}${suffix}` : Str
TypeScript类型体操心法2--模式匹配做提取
于 2024-07-15 08:33:02 首次发布