type GetFirst<Arr extends unknown[]> = Arr extends [infer First, ...unknown[]] ? First : never;
type GetLast<Arr extends unknown[]> = Arr extends [...unknown[],infer Last] ? Last : never;
type PopArr<Arr extends unknown[]> = Arr extends [] ? [] : Arr extends [...infer Result,unknown] ? Result : never;
type ShiftArr<Arr extends unknown[]> = Arr extends [] ? [] : Arr extends [unknown,...infer Result] ? Result : never;
type StartWith<Str extends string, Start extends string> = Str extends `${Start}${string}` ? true : false;
type ReplaceStr<
Str extends string,
From extends string,
To extends string
> = Str extends `${infer Prefix}${From}${infer Suffix}` ?
ReplaceStr<`${Prefix}${To}${Suffix}`,From,To> : Str
type TrimStringRight<Str extends string> = Str extends `${infer Result}${' '}` ? TrimStringRight<Result> : Str;
type TrimStringLeft<Str extends string> = Str extends `${' '}${infer Result}` ? TrimStringLeft<Result> : Str;
type TrimString<Str extends string> = Str extends `${' '}${infer Result}${' '}` ? TrimString<Result> : Str;
type FunctionArgs<Fn extends Function> = Fn extends (...args:infer Args) => unknown ? Args : never;
type FunctionResult<Fn extends Function> = Fn extends (...args:any[]) => infer Result ? Result : never;
type FunctionThis<Fn extends Function> = Fn extends (this:infer This,...args:unknown[])=>unknown ? This : never;
type GetInstanceType<Type extends new(...args:any[])=>any> = Type extends new(...args:any) => infer Result ? Result : never;
// 测试案例
interface Person{
name: string
}
interface PersonConstructor{
new(name:string):Person
}
type person = GetInstanceType<PersonConstructor>;
type GetInstanceParam<Type extends new(...args:any[])=>any> = Type extends new(...args:infer Result) => unknown ? Result : never;
// 测试案例
interface Person{
name: string
}
interface PersonConstructor{
new(name:void):Person
}
type person = GetInstanceParam<PersonConstructor>;
type Push<Arr extends unknown[],Ele> = [...Arr,Ele];
type Shift<Ele, Arr extends unknown[]> = [Ele,...Arr];
type Concat<One extends [unknown,unknown],Two extends [unknown,unknown]> =
One extends [infer OneFirst, infer OneSecond] ?
Two extends [infer TwoFirst, infer TwoSecond] ? [[OneFirst,OneSecond],[TwoFirst,TwoSecond]] : []
: []
type Concat<One extends unknown[],Two extends unknown[]> =
One extends [infer OneFirst,...infer OneOther] ?
Two extends [infer TwoFirst,...infer TwoOther] ? [[OneFirst,TwoFirst],...Concat<OneOther,TwoOther>] : []
: [];
type StringFistToUp<Str extends string> = Str extends `${infer First}${infer Other}` ? `${Uppercase<First>}${Other}` : never
type DeleteString<Str extends string,Ele extends string> =
Str extends `${infer Start}${Ele}${infer End}` ? DeleteString<`${Start}${End}`,Ele> : Str
type FunAddParam<Fn extends Function,Arg extends unknown> =
Fn extends (...args:infer Args)=>infer Result ? (...args:[...Args,Arg])=>Result : never
type KeyUp<Obj extends Object> = {
[Key in keyof Obj as Uppercase<Key & string>]:Obj[Key];
}
type _Record<K extends keyof any, T> = {
[P in K]: T
}
type ReadonlyParam<T> = {
readonly [P in keyof T]: T[P]
}
type toSelector<T> = {
[P in keyof T]?: T[P]
}
type RemoveReadonly<T> = {
-readonly [P in keyof T]: T[P]
}
type ClearSelector<T> = {
[P in keyof T]-?: T[P]
}
type FilterValue<T extends Record<string,any>,valueType extends unknown> = {
[Key in keyof T as T[Key] extends valueType ? Key : never]: T[Key]
}
type PromiseValue<T> =
T extends Promise<infer Value>?
PromiseValue<Value>
: T
type promise = PromiseValue<Promise<Promise<Record<string,any>>>>
type ReverseArr<T extends Array<unknown>> = T extends [infer First,...infer Other] ? [...ReverseArr<Other>,First] : T;
type IsEqual<A,B> = (A extends B ? true : false) & (B extends A ? true : false);
type Include<Arr extends unknown[],Ele> =
Arr extends [infer First,...infer Other] ?
IsEqual<First,Ele> extends true ? true : Include<Other,Ele>
: false;
type RemoveArrItem<
Arr extends unknown[],
Item,
Result extends unknown[] = []
> = Arr extends [infer First,...infer Other] ?
IsEqual<Item,First> extends true ? RemoveArrItem<Other,Item,Result> : RemoveArrItem<Other,Item,[...Result,First]>
: Result
type IsEqual<A,B> = (A extends B ? true : false) & (B extends A ? true : false)
type BuildArr<
Length extends number,
Ele,
Arr extends unknown[] = []
> = Arr['length'] extends Length ? Arr : BuildArr<Length,Ele,[...Arr,Ele]>
type Replace<
Str extends string,
From extends string,
To extends string
> = Str extends `${infer Start}${From}${infer End}` ? Replace<`${Start}${To}${End}`,From,To> : Str
type StrType<Str extends string,Result = never> = Str extends `${infer First}${infer Other}` ? StrType<Other,Result|First> : Result
type ReverseString<Str extends string,Result extends string = ""> = Str extends `${infer First}${infer Other}` ? ReverseString<Other,`${Result}${First}`> : Result
-递归将所有层的索引都变成只读
错误代码,如果测试一下我们就会发现,递归没有进行下去。因为ts的类型只有被用到的时候才会被计算。这里只是给第一次的索引加上了只读,但是没有使用。所以不会进行计算,我们可以加一个Obj extends any让其计算。
type DeepReadonly<Obj extends Record<string,any>> = Obj extends any ? {
readonly [Key in keyof Obj]: Obj[Key] extends Record<string,any> ?
Obj[Key] extends Function ? Obj[Key] : DeepReadonly<Obj[Key]>
: Obj[Key]
} : never
type BuildArray<
Length extends number,
Ele = unknown,
Result extends unknown[] = []
> = Result['length'] extends Length ? Result : BuildArray<Length,Ele,[Ele,...Result]>
type Add<Arr extends unknown[],Result extends unknown[] = []> = Arr extends [infer Start extends number,...infer Other]
? Add<Other,[...BuildArray<Start>,...Result]> :
Result['length']
type s = Add<[1,2,3]>
type BuildArray<
Length extends number,
Ele = unknown,
Result extends unknown[] = []
> = Result['length'] extends Length ? Result : BuildArray<Length,Ele,[Ele,...Result]>
type SubTract<Num1 extends number,Num2 extends number> = BuildArray<Num1> extends [...BuildArray<Num2>,...infer Result] ? Result['length'] : never
type BuildArray<
Length extends number,
Ele = unknown,
Result extends unknown[] = []
> = Result['length'] extends Length ? Result : BuildArray<Length,Ele,[Ele,...Result]>
type SubTract<Num1 extends number,Num2 extends number> = BuildArray<Num1> extends [...BuildArray<Num2>,...infer Result] ? Result['length'] : never
type Multiply<
Num1 extends number,
Num2 extends number,
Result extends unknown[] = []
> = Num1 extends 0 ? Result['length'] : Multiply<SubTract<Num1,1>,Num2,[...Result,...BuildArray<Num2>]>
type BuildArray<
Length extends number,
Ele = unknown,
Arr extends unknown[] = []
> = Arr['length'] extends Length ?
Arr :
BuildArray<Length,Ele,[...Arr,Ele]>
type Subtract<Num1 extends number, Num2 extends number> = BuildArray<Num1> extends [...arr1: BuildArray<Num2>, ...arr2: infer Rest] ?
Rest['length'] : never
type Divide<
Num1 extends number,
Num2 extends number,
ResultArr extends unknown[] = []
> = Num1 extends 0 ?
ResultArr['length'] :
Divide<Subtract<Num1,Num2>,Num2,[unknown, ...ResultArr]>;
type divide = Divide<6,3>;
type StrLen<
Str extends string,
ResultArr extends unknown[] = []
> = Str extends `${string}${infer Rest}`?
StrLen<Rest,[...ResultArr, unknown]>
: ResultArr['length'];
type str = StrLen<'123'>;
type Than<
Num1 extends number,
Num2 extends number,
Count extends unknown[] = []
> = Num1 extends Num2 ? false :
Count['length'] extends Num2 ? true :
Count['length'] extends Num1 ? false :
Than<Num1,Num2,[unknown,...Count]>
type Fi<
Left extends unknown[],
Right extends unknown[],
Num extends number,
Idx extends unknown[] = [],
> = Idx['length'] extends Num ? Right['length'] : Fi<Right,[...Left,...Right],Num,[...Idx,unknown]>
type Fib<Num extends number> = Fi<[],[1],Num,[1]>
type Tool<Str extends string> = Str extends `${infer First}_${infer Start}${infer End}` ? Tool<`${First}${Uppercase<Start>}${End}`> : Str
type item = Tool<'aa_bb_cc_dd'>;
type Tool<A, B = A> =
A extends A?
[B] extends [A] ? false : true
: never
type bemResult = BEM<'guang', ['aaa', 'bbb'], ['warning', 'success']>;
//会转换成
guang__aaa--warning guang__bbb--success
type BEM<Ele1 extends string,Ele2 extends string[],Ele3 extends string[]> = `${Ele1}__${Ele2[number]}--${Ele3[number]}`
type Combination<A extends string, B extends string> =
| A
| B
| `${A}${B}`
| `${B}${A}`;
type AllCombinations<A extends string, B extends string = A> =
A extends A
? Combination<A, AllCombinations<Exclude<B, A>>>
: never;
type all = AllCombinations<'A' | 'B' | 'C'>;
type IsEqual<A,B> = (<T>()=>T extends A ? 1 : 2) extends (<T>()=>T extends B ? 1 : 2) ? true : false
type Test<T> = T extends number ? 1 : 2;
type res = Test<1 | 'a'>;//type res = 1 | 2
type Test<T> = T extends true ? 1 : 2;
type res = Test<boolean>;//type res = false | true
type Test<T> = T extends true ? 1 : 2;
type res = Test<any>;//type res = 1 | 2
type Test<T> = T extends true ? 1 : 2;
type res = Test<never>;//type res = never
type Parameters<T extends (...args: any) => any> = T extends (...args: infer P) => any ? P : any;
type ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any;
type ConstructorParameters<T extends abstract new (...args: any) => any> = T extends abstract new (...args: infer P) => any ? P : never;
type InstanceType<T extends abstract new (...args: any) => any> = T extends abstract new (...args: any) => infer R ? R : any;
type Partial<T> = {
[P in keyof T]?: T[P];
};
type Required<T> = {
[P in keyof T]-?: T[P];
};
type Readonly<T> = {
readonly [P in keyof T]: T[P];
};
type Pick<T, K extends keyof T> = {
[P in K]: T[P];
};
type Record<K extends keyof any, T> = {
[P in K]: T;
};
type Exclude<T, U> = T extends U ? never : T;
type Extract<T, U> = T extends U ? T : never;
type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
type Awaited<T> =
T extends null | undefined
? T
: T extends object & { then(onfulfilled: infer F): any }
? F extends ((value: infer V, ...args: any) => any)
? Awaited<V>
: never
: T;
type NonNullable<T> = T extends null | undefined ? never : T;
type ThisParameterType<T> =
T extends (this: infer U, ...args: any[]) => any
? U
: unknown;
type OmitThisParameter<T> =
unknown extends ThisParameterType<T>
? T
: T extends (...args: infer A) => infer R
? (...args: A) => R
: T;