extends
inheritance
export type Extends<T,U> = T extends U;
Copy the code
readonly
read-only
export type Readonly<T> = { readonly [P in keyof T]: T[P] };
Copy the code
is
是
export type IsType<T> = (val: any) = > val is T;
Copy the code
Scenario: Determine whether it belongs to a certain value or a class of values
/ / if the objectexportconst isObj: IsType<object>; / / whether the numberexportconst isNumber: IsType<number>; / / whether the stringexport const isString: IsType<string>;
Copy the code
keyof
The key value
export type KeyOf<T> = keyof T;
Copy the code
Use K extends Keyof WindowEventMap to restrict type:K to the list of keys in WindowEventMap and ev in listener to the value of K corresponding to the WindowEventMap
declare function addEventListener<K extends keyof WindowEventMap>(type: K, listener: (this: Window, ev: WindowEventMap[K]) => any, options? : boolean | AddEventListenerOptions): void;Copy the code
in
In the *
export type Partial<T> = { [P inkeyof T]? : T[P] };Copy the code
?
Two morphemes
export type Extract<T, U> = T extends U ? T : never;
Copy the code
&
merge
export type Merge<T,U> = T & U;
Copy the code
practical
export type IsType<T = any> = (val: any) = > val is T;
export type KeyOf<T> = keyof T;
export type Defaultize<P, D> = P extends any
? string extends keyof P
? P
: Pick<P, Exclude<keyof P, keyof D>> &
Partial<Pick<P, Extract<keyof P, keyof D>>> &
Partial<Pick<D, Exclude<keyof D, keyof P>>>
: never;
export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
export type Matching<InjectedProps, DecorationTargetProps> = {
[P in keyof DecorationTargetProps]: P extends keyof InjectedProps
? InjectedProps[P] extends DecorationTargetProps[P]
? DecorationTargetProps[P]
: InjectedProps[P]
: DecorationTargetProps[P]
};
export type MergePropTypes<P, T> = P & Pick<T, Exclude<keyof T, keyof P>>;
export type Partial<T> = { [P inkeyof T]? : T[P] };export type Required<T> = { [P inkeyof T]-? : T[P] };export type Readonly<T> = { readonly [P in keyof T]: T[P] };
export type Pick<T, K extends keyof T> = { [P in K]: T[P] };
export type Record<K extends keyof any, T> = { [P in K]: T };
export type Exclude<T, U> = T extends U ? never : T;
export type Extract<T, U> = T extends U ? T : never;
export type NonNullable<T> = T extends null | undefined ? never : T;
export type ReturnType<T extends(... args:any[]) = >any> = T extends (
...args: any[]
) => infer R
? R
: any;
export type InstanceType<
T extends new(... args:any[]) = >any
> = T extends new(... args:any[]) => infer R ? R : any;
Copy the code