TypeScript 提供了一些内置的实用类型,可以更好的方式将类型从一种形式转换到另一种形式。

创新互联公司是一家专注于网站设计制作、成都网站设计与策划设计,上林网站建设哪家好?创新互联公司做网站,专注于网站建设十余年,网设计领域的专业建站公司;建站业务涵盖:上林等地区。上林做网站价格咨询:18982081108
这些内置的类型全局可用的,所以可以很方便的使用它们。
在了解 TypeScript 实用、类型之前,类型别名和泛型很重要。我们以在TypeScript中为任何现有类型创建类型别名。
- type MyString = string;
 - let helloWorldMessage: MyString = 'Hello Wisdom Geek';
 
泛型用于创建可重用的类型别名。假设我们有一个identity 函数,该函数返回传递的任何值:
- const identity = (arg: string): string => arg;
 
如果我们要返回的是 number 类型怎么办?有小伙伴可能会用any代替特定的类型
- const identity = (arg: any): any => arg;
 
但这减少了参数的类型信息,因此,也失去 TypeScript 带来的好处。我们想要以一种可以用来表示返回类型的方式来捕获参数的类型。这就是泛型派上用场的地方。我们将使用适用于类型而不是值的类型变量。
- const identity
 = (arg: T): T => arg; 
接着,在调用它时指定函数的类型:
- const output = identity
 ("Hello Wisdom Geek"); 
在开始讲解内置实用类型之前,这些工具类型在4.0版本之前是可用的,不需要任何额外的包。
Pritial
- type BlogPost = {
 - title: string;
 - author: string;
 - }
 - type PartialBlogPost = Partial
 ; - /* 等价于 {
 - title?: string;
 - author?: string;
 - } */
 
Required
- type PartialBlogPost = {
 - title?: string;
 - author?: string;
 - }
 - type BlogPost = Required
 ; - /* 等价于 {
 - title: string;
 - author: string;
 - } */
 
Readonly
- type BlogPost = {
 - title: string;
 - author: string;
 - }
 - type BlogPost = Readonly
 ; - /* 等价于 {
 - readonly title: string;
 - readonly author: string;
 - } */
 
Pick
- type Point3D = {
 - x: number,
 - y: number,
 - z: number,
 - };
 - type Point2D = Pick
 ; - /* 等价于 {
 - x: number,
 - y: number
 - } */
 
Parameters
- type T0 = Parameters<() => string>;
 - // type T0 = []
 - type T1 = Parameters<(s: string) => void>;
 - // type T1 = [s: string]
 - type T2 = Parameters<
 (arg: T) => T>; - // type T2 = [arg: unknown]
 
Omit
- type Point3D = {
 - x: number,
 - y: number,
 - z: number,
 - };
 - type Point2D = Omit
 ; - /* same as {
 - x: number,
 - y: number
 - } */
 
Record
- type BlogPost = Record<'title' | 'author', strnig>
 - /* same as {
 - title: string;
 - author: string;
 - } */
 
如果所有类型都具有相同的值,则声明的 Record 版本会更加简洁和可读,因为它们都具有相同的类型。
Extract
- type T0 = Extract<"a" | "b" | "c", "a" | "f">;
 - // type T0 = "a"
 - type T1 = Extract
 void), Function>; - // type T1 = () => void
 
Exclude
- type T0 = Exclude<"a" | "b" | "c", "a">;
 - // type T0 = "b" | "c"
 - type T1 = Exclude
 void), Function>; - // type T2 = string | number
 
NonNullable
- type T0 = NonNullable
 ; - // type T0 = string | number
 - type T1 = NonNullable
 ; - // type T1 = string[]
 
ReturnType
- type T0 = ReturnType<() => string>;
 - type T0 = string
 - type T1 = ReturnType<(s: string) => void>;
 - type T1 = void
 - type T2 = ReturnType<
 () => T>; - type T2 = unknown
 - type T3 = ReturnType<
 () => T>; - type T3 = number[]
 - type T5 = ReturnType
 ; - type T5 = any
 - type T6 = ReturnType
 ; - type T6 = never
 - type T7 = ReturnType
 ; 
InstanceType
- class C {
 - x = 0;
 - y = 0;
 - }
 - type T0 = InstanceType
 ; - type T0 = C
 - type T1 = InstanceType
 ; - type T1 = any
 - type T2 = InstanceType
 ; - type T2 = never
 
~完,我是小智。
更多实用类别,请自行看官网。https://www.typescriptlang.org/docs/handbook/utility-types.html#uppercasestringtype
作者:SARANSH KATARIA 译者:前端小智 来源:wisdomgeek
原文:https://www.wisdomgeek.com/development/web-development/typescript/using-utility-types-for-transforming-typescript-types/
本文转载自微信公众号「大迁世界」,可以通过以下二维码关注。转载本文请联系大迁世界公众号。