
英文 | https://javascript.plainenglish.io/15-utility-types-that-every-typescript-developer-should-know-6cf121d4047c

/*** Make all properties in T optional.* typescript/lib/lib.es5.d.ts*/type Partial<T> = {[P in keyof T]?: T[P];};


/*** Make all properties in T required.* typescript/lib/lib.es5.d.ts*/type Required<T> = {[P in keyof T]-?: T[P];};


/*** Make all properties in T readonly.* typescript/lib/lib.es5.d.ts*/type Readonly<T> = {readonly [P in keyof T]: T[P];};


/*** Construct a type with a set of properties K of type T.* typescript/lib/lib.es5.d.ts*/type Record<K extends keyof any, T> = {[P in K]: T;};

/*** Exclude from T those types that are assignable to U.* typescript/lib/lib.es5.d.ts*/type Exclude<T, U> = T extends U ? never : T;


/*** Extract from T those types that are assignable to U.* typescript/lib/lib.es5.d.ts*/type Extract<T, U> = T extends U ? T : never;


/*** From T, pick a set of properties whose keys are in the union K.* typescript/lib/lib.es5.d.ts*/type Pick<T, K extends keyof T> = {[P in K]: T[P];};


/*** Construct a type with the properties of T except for those* in type K.* typescript/lib/lib.es5.d.ts*/type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;


/*** Exclude null and undefined from T.* typescript/lib/lib.es5.d.ts*/type NonNullable<T> = T extends null | undefined ? never : T;

/*** Obtain the parameters of a function type in a tuple.* typescript/lib/lib.es5.d.ts*/type Parameters<T extends (...args: any) => any> = T extends(...args: infer P) => any ? P : never;

/*** Obtain the return type of a function type.* typescript/lib/lib.es5.d.ts*/type ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any;




ConstructorParameters<Type>:根据构造函数类型的类型构造元组或数组类型。它产生一个包含所有参数类型的元组类型(如果 Type 不是函数,则类型 never )。 InstanceType<Type>:构造一个由Type中构造函数的实例类型组成的类型。 ThisParameterType<Type>:为函数类型提取此参数的类型,如果函数类型没有此参数,则为未知。
学习更多技能
请点击下方公众号
![]()


本文来自投稿,不代表本站立场,如若转载,请注明出处:https://typecho.firshare.cn/archives/1018.html
免责声明:文章内容不代表本站立场,本站不对其内容的真实性、完整性、准确性给予任何担保、暗示和承诺,仅供读者参考,文章版权归原作者所有。避免网络欺诈,本站不倡导任何交易行为。如您私自与本站转载自公开互联网中的资讯内容中提及到的个人或平台产生交易,则需自行承担后果。本站在注明来源的前提下推荐原文至此,仅作为优良公众、公开信息分享阅读,不进行商业发布、发表及从事营利性活动。如本文内容影响到您的合法权益(内容、图片等),请及时联系本站,我们会及时删除处理。
