export type Dispatch<T> = T extends (...args: infer A) => any ? (...args: A) => void : never;
type T0 = Dispatch<() => string>; // () => void, 里面 A 就是[]
type T1 = Dispatch<(s: string) => void> // (s: string) => void, A 就是 [string]
type T2 = Dispatch<(<T>(arg: T) => T)>; // (arg: unknow) => void, A 就是[unknow]
type T3 = Dispatch<object>; // never
// 这么传跟已知类型传其实没太大区别
type Parameters1<T, R extends Array<any>> = T extends (...args: R) => any ? R : any;
type T4 = Parameters1<(s: string) => void, number[]>;
// 函数类型
interface SearchFunc {
(source: string, subString: string): boolean;
}
// 对象/方法
type Connected = {
delay(input: number): string;
setMessage(action: Date): string;
};
// ******* 类型分配 ******
class EffectModule {
a = 1;
b() {
}
}
interface Action<T> {
payload?: T;
type: string;
}
// 获取方法的名称
type MethodName<T> = {[F in keyof T]: T[F] extends Function ? F : never}[keyof T]
type EE = MethodName<EffectModule>
type asyncMethod<T, U> = (input: Promise<T>) => Promise<Action<U>>
type asyncMethodConnect<T, U> = (input: T) => Action<U>
type syncMethod<T, U> = (action: Action<T>) => Action<U>
type syncMethodConnect<T, U> = (action: T) => Action<U>
type EffectMethodAssign<T> = T extends asyncMethod<infer U, infer V> ? asyncMethodConnect<U, V> : T extends syncMethod<infer U, infer V> ? syncMethodConnect<U, V> : never
type Connect = (module: EffectModule) => {
[F in MethodName<typeof module>]:EffectMethodAssign<typeof module[F]>
}
//********* */ keyof 用法 **********
interface Itest{
webName:string;
age: number;
address:string;
dd: never;
}
type ant = keyof Itest; // ant = "webName" | "age" | "address"| 'dd'
type d = Itest[keyof Itest] // string | number , 这里 never 就被剔除
// ********** extends 的 条件类型 *************
// extends 运用在 type 和 class 中时完全是两种作用的效果
// 简单的值匹配
type Equal<X, Y> = X extends Y ? true : false;
type Str = Equal<'a', 'a'>; // true
type Boo = Equal<true, false>; // false
// 简单的类型匹配
type isNum<T> = T extends number ? number : string
//判断联合类型
type A = 'x';
type B = 'x' | 'y';
type Y = A extends B ? true : false; // true
// 当联合类型无法做出判断时
type AB<T> = T extends 'x' ? 'a' : 'b';
type C = AB<'x'>;
// 得到 type C = 'a'
type All = AB<'x' | 'y'>; // 非确定条件,可能是 'x' 或 'y'
// 得到 type All = 'a' | 'b';
//泛型约束
interface Lengthwise {
length: number;
a: string;
}
function loggingIdentity<T extends Lengthwise>(arg: T): T {
console.log(arg.length); // Now we know it has a .length property, so no more error
return arg;
}
//
type p = 'length' extends keyof Lengthwise ? true : false; // true;
// *** 高级类型 ***
// Partial<T>, Readonly<T>,
// Record<K extends keyof any, T>,
// Pick<T, K extends keyof T> ,
// Exclude<T, U> 相反的Extract<T, U>
// 排除接口中指定的属性 Omit<T, K extends keyof any>
type record = Record<'x' | 'y', number>; //{x: number, y; number}
// NonNullable< T >
type Tp1 = NonNullable<string | null | undefined>; // string
// Parameters 获取函数的全部参数类型,以 元组类型 返回:
// Parameters<T extends (...args: any) => any> = T extends (...args: infer P) => any ? P : never;
type F1 = (a: string, b: number) => void;
type F1ParamTypes = Parameters<F1>; // [a:string, b:number]
// ReturnType 接收函数声明,返回函数的返回值类型,如果多个类型则以 联合类型 方式返回
//type ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any;
type F2 = () => Date;
type F1ReturnType = ReturnType<F1>; // Date
// InstanceType 只是这里获取的是 构造函数 的全部参数
// type InstanceType<T extends new (...args: any) => any> = T extends new (...args: any) => infer R ? R : any;