15 种TypeScript最常用的实用程序类型
source link: https://www.51cto.com/article/721180.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
为了方便 TypeScript 用户,TypeScript 开发团队为我们提供了许多有用的内置实用程序类型。通过这些实用类型,我们
可以轻松地转换类型、提取类型、排除类型,或者获取函数的参数类型或返回值类型。
在本文中,我从 TypeScript 的内置实用程序类型中挑选了 15 种非常有用的类型,并以图像的形式介绍了它们的用法和内部工作原理,看完这篇文章,相信你可以真正掌握这些内置实用程序类型的用法。
1.Partial<Type>
构造一个类型,其中 Type 的所有属性都设置为可选。
/** * Make all properties in T optional. * typescript/lib/lib.es5.d.ts */type Partial<T> = { [P in keyof T]?: T[P];};
2. Required<Type>
构造一个类型,该类型由设置为 required Type 的所有属性组成,部分的反义词。
/** * Make all properties in T required. * typescript/lib/lib.es5.d.ts */type Required<T> = { [P in keyof T]-?: T[P];};
3.Readonly<Type>
构造一个 Type 的所有属性都设置为 readonly 的类型,这意味着构造类型的属性不能被重新分配。
/** * Make all properties in T readonly. * typescript/lib/lib.es5.d.ts */type Readonly<T> = { readonly [P in keyof T]: T[P];};
4.Record<Keys, Type>
构造一个对象类型,其属性键为 Keys,其属性值为 Type,此实用程序可用于将一种类型的属性映射到另一种类型。
/** * 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;};
5. Exclude<UnionType, ExcludedMembers>
通过从 UnionType 中排除可分配给 ExcludedMembers 的所有联合成员来构造类型。
/** * 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;
6. Extract<Type, Union>
通过从 Type 中提取所有可分配给 Union 的联合成员来构造一个类型。
/** * 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;
7. Pick<Type, Keys>
通过从 Type 中选择一组属性 Keys(字符串文字或字符串文字的联合)来构造一个类型。
/** * 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];};
8.Omit<Type, Keys>
通过从 Type 中选择所有属性然后删除 Keys(字符串文字或字符串文字的联合)来构造一个类型。
/** * 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>>;
9. NonNullable<Type>
通过从 Type 中排除 null 和 undefined 来构造一个类型。
/** * Exclude null and undefined from T. * typescript/lib/lib.es5.d.ts */type NonNullable<T> = T extends null | undefined ? never : T;
10. Parameters<Type>
从函数类型 Type 的参数中使用的类型构造元组类型。
/** * 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;
11. ReturnType<Type>
构造一个由函数 Type 的返回类型组成的类型。
/** * 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;
12. Uppercase<StringType>
将字符串文字类型转换为大写。
13.小写<StringType>
将字符串文字类型转换为小写。
14. 大写<StringType>
将字符串文字类型的第一个字符转换为大写。
15. 取消大写<StringType>
将字符串文字类型的第一个字符转换为小写。
除了上述这些实用程序类型之外,还有一些其他常用的 TypeScript 内置实用程序类型,具体如下:
- ConstructorParameters<Type>:根据构造函数类型的类型构造元组或数组类型。它产生一个包含所有参数类型的元组类型(如果 Type 不是函数,则类型 never )。
- InstanceType<Type>:构造一个由Type中构造函数的实例类型组成的类型。
- ThisParameterType<Type>:为函数类型提取此参数的类型,如果函数类型没有此参数,则为未知。
本文介绍的实用程序类型在内部使用了有关映射类型、条件类型和推断类型推断的知识。如果你对映射类型和条件类型不熟悉,后面我将继续分享一些这个方面的知识。
Recommend
-
37
现实生活中,对电脑越是熟悉的网工们懂得的快捷命令越多。今天跟大家讲讲Windows系统常见的快捷命令。
-
25
Vim按键映射可用于设置Vim命令或命令串的别名,完成个性化Vim按键设置。 Vim命令虽然很多,但是常用的Vim命令却被使用者所熟知。通过 Vim 按键映射,可减少按键次数、提供Vim的工作效率。 本文介绍15种常用的Vim按键...
-
17
常用Vim命令及实用Vim按键映射配置详解(2019年最新) Vim按键映射可...
-
3
typescript 实用小技巧小纪同学 Posted at — Aug 24, 2021 阅读 3用了很久的 typescript,用了但感觉又没完全用。因为很多 typescript 的特性没有被使用,查看之前写的代码满屏的 any,这样就容易导致很多 bug,也没有发挥出 ty...
-
7
VSCode实用配置及常用快捷键推荐安装的插件:Code RunnerGit GraphGo for Visual Studio CodeMaterail Icon Theme通用配置:
-
5
13 个Typescript 实用类型:开发人员的备忘单更新日期: 2022-05-25阅读量: 20标签:
-
0
TypeScript 是 JS 的超集,TS 提供了 JS 的所有功能,并且额外的增加了:类型系统 JS 有类型(比如,number/string 等),但是 JS 不会检查变量的类型是否发生变化,而 TS 会检查 TypeScript 类型...
-
0
原始类型:string,number,booleanjavaScript 有三个经常会使用到的原始类型:stirng,number,boolean。每一个在Typescript都有一个相对应的类型。 正如你所期望的,你可以得到相同的名称在javascript中使用typeof去操作这些类型对应的值:strin...
-
5
12 个Typescript开发实用小技巧【值得收藏】更新日期: 2023-01-30阅读: 21标签:
-
0
引言
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK