TS currently supports a lot of https://www.typescriptlang.org/docs/handbook/utility-types.html
But it's still not enough. Let me share a few common methods of my own packaging.
intersection
type Intersection<T extends object, U extends object> = Pick<
T,
Extract<keyof T, keyof U> & Extract<keyof U, keyof T>
>;
difference set
type Diff<T extends object, U extends object> = Omit<
T & U,
keyof Intersection<T, U>
>;
the specified attribute to Optional
type PartialKey<U extends object, k extends keyof U> = Partial<U> & Omit<U, k>;
changes the specified attribute to Required
type RequiredKey<U extends object, K extends keyof U> = U &
Required<Pick<U, K>>;
Get the array element type
type ArrayItem<T extends unknown[]> = T extends (infer P)[] ? P : never;
Get the value type in the Record
type RecordValueType<U extends Record<any, unknown>> = U extends Record<any, infer P> ? P : never;
Get Promise return type
type PromiseReturnType<U extends Promise<unknown>> = U extends Promise<infer P> ? P : never;
Code: https://stackblitz.com/edit/typescript-ug3pat?file=index.ts
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。