数组类型的递归
type DeepPromiseValueType<P extends Promise<unknown>> =
P extends Promise<infer ValueType>
? ValueType extends Promise<unknown>
? DeepPromiseValueType<ValueType>
: ValueType
: never
type res1 = DeepPromiseValueType<Promise<Promise<Promise<Record<string, any>>>>>
type DeepPromiseValueType2<T> =
T extends Promise<infer ValueType>
? DeepPromiseValueType2<ValueType>
: T;
type res2 = DeepPromiseValueType2<Promise<Promise<number>>>
type res3 = Record<string, any>
type ReverseArr<Arr extends unknown[]> =
Arr extends [infer First, ...infer Rest]
? [...ReverseArr<Rest>, First]
: Arr;
type res4 = ReverseArr<[1,2,3,4,5]>
type IsEqual<A, B> = (A extends B ? true : false) & (B extends A ? true : false)
type Includes<Arr extends unknown[], FindItem> =
Arr extends [infer First, ...infer Rest]
? IsEqual<First, FindItem> extends true
? true
: Includes<Rest, FindItem>
: false;
type res5 = Includes<[1, 2, 3, 44, 5, {a: 11, b: 2}], {a: 11}>
type BuildArray<
Length extends number,
Ele = unknown,
Arr extends unknown[] = []
> = Arr['length'] extends Length
? Arr : BuildArray<Length, Ele, [...Arr, Ele]>
type res6 = BuildArray<3>
// 字符串
type ReplaceStr<
Str extends string,
From extends string,
To extends string,
> = Str extends `${infer Prefix}${From}${infer Suffix}`
? `${Prefix}${To}${Suffix}` : Str;
type ReplaceAll<
Str extends string,
From extends string,
To extends string
> = Str extends `${infer Left}${From}${infer Right}`
? `${Left}${To}${ReplaceAll<Right, From, To>}`
: Str;