日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
一看就懂的TypeScript工具類型

TypeScript是一種靜態(tài)類型檢查的編程語言,它內(nèi)置了許多基本數(shù)據(jù)類型,如字符串、數(shù)字和布爾型等。除了基本數(shù)據(jù)類型,當(dāng)某種類型對于大多數(shù)代碼來說都非常有用時,它們就會被添加到TypeScript中并且被大家使用而無需擔(dān)心它們的可用性。這些內(nèi)置在TS中的類型我們稱之為工具類型,這些工具類型位于TS安裝目錄typescript/lib/lib.es5.d.ts,熟悉這些工具類型,可以幫助我們提高開發(fā)效率。

創(chuàng)新互聯(lián)建站從2013年創(chuàng)立,先為麟游等服務(wù)建站,麟游等地企業(yè),進行企業(yè)商務(wù)咨詢服務(wù)。為麟游企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

Partial、Required 與 Readonly

該組工具類型為改操作的工具類型,具體為將類型T的所有屬性都改為可選、必選或只讀。

定義:

/**
 * Make all properties in T optional
 */
type Partial = {
    [P in keyof T]?: T[P];
};
/**
 * Make all properties in T required
 */
type Required = {
    [P in keyof T]-?: T[P];
};
/**
 * Make all properties in T readonly
 */
type Readonly = {
    readonly [P in keyof T]: T[P];
};

知識點:

in:關(guān)鍵字,用來實現(xiàn)遍歷;

keyof:關(guān)鍵字,索引類型查詢,用來獲取類型的所有鍵,返回的類型是聯(lián)合類型;

?:修飾符,表示可選屬性;

readonly:修飾符,表示只讀屬性;

-:修飾符,添加在“?”或"readonly"修飾符之前,表示移除“?”或"readonly"修飾符。

作用:

Partial,將T類型中的所有屬性變?yōu)榭蛇x屬性; Required,將T類型中的所有屬性變?yōu)楸剡x屬性; Readonly,將T類型中的所有屬性變?yōu)橹蛔x屬性。

應(yīng)用:

interface Text {
  size: number
  color: string
}
type T = Partial
type R = Required
type O = Readonly

新定義的T類型中的屬性均為Text類型屬性,且均為可選;新定義的R類型中的屬性均為Text類型屬性,且均為必選;新定義的O類型中的屬性均為Text類型屬性,且均為只讀。

Record

該類型可視作增操作相關(guān)的工具類型,根據(jù)我們指定的鍵值類型,新增一個對象類型。

定義:

/**
 * Construct a type with a set of properties K of type T
 */
type Record = {
    [P in K]: T;
};

知識點:

keyof any:上面介紹過keyof(關(guān)鍵字,用來獲取類型的所有鍵,返回的類型是聯(lián)合類型),當(dāng)對any使用keyof索引類型查詢時,結(jié)果類型為固定的聯(lián)合類型“string | number | symbol”;

K extends keyof any:泛型約束,定義了類型K的最大范圍為聯(lián)合類型“string | number | symbol”。

作用:

根據(jù)給定的屬性名類型和屬性類型創(chuàng)建一個新的對象類型。

應(yīng)用:

type K = 'size'|'color'
type T = number
type R = Record

新定義的R類型,包括屬性size和color,且類型均為number。

Exclude 與 Extract

該組類型可以視作查操作相關(guān)的工具類型,查出T類型中與U類型無關(guān)的屬性或相關(guān)的屬性。

定義:

/**
 * Exclude from T those types that are assignable to U
 */
type Exclude = T extends U ? never : T;

/**
 * Extract from T those types that are assignable to U
 */
type Extract = T extends U ? T : never;

知識點:

T extends U ? X : Y:條件類型,extends是關(guān)鍵字,若類型T能夠賦值給類型U,則條件類型的結(jié)果為類型X,否則為類型Y。

作用:

根據(jù)條件類型的定義,Exclude類型中若T類型中的屬性存在于U類型,則返回never,也就是從類型T中剔除所有類型U的屬性。Extract則恰恰和Exclude相反,返回類型T和類型U的交集。

應(yīng)用:

interface Text {
  size: number
  color: string
}
interface Img {
  width: number
  color: string
}
type T = Exclude
type R = Extract

新定義的T類型,只有size屬性;新定義的R類型,只包含color屬性。

Pick、Omit與NonNullable

該組工具類型為刪操作相關(guān)的工具類型,包括剔除與指定鍵相關(guān)、無關(guān)或null、undefined類型操作的工具類型。

定義:

/**
 * From T, pick a set of properties whose keys are in the union K
 */
type Pick = {
    [P in K]: T[P];
};
/**
 * Construct a type with the properties of T except for those in type K.
 */
type Omit = Pick>;
/**
 * Exclude null and undefined from T
 */
type NonNullable = T extends null | undefined ? never : T;

作用:

Pick類型從已有對象類型T中選取選定的屬性及其類型K創(chuàng)建新的類型。Omit與Pick類型相反,從已有對象類型T中剔除選定的屬性及其類型K創(chuàng)建新的類型。NonNullable與Omit相似,返回的結(jié)果為從T類型中剔除null and undefined類型。

應(yīng)用:

interface Text {
  size: number
  color: string
}
type T = Pick
type R = Omit
type N = NonNullable

新定義的T類型,只包括Text類型中的屬性size及其類型;新定義的T類型,只包括Text類型中的屬性color及其類型;新定義的N類型,只包括Text類型。

Parameters、ConstructorParameters、ReturnType與InstanceType

該組工具類型為與函數(shù)相關(guān)的工具類型,包括獲取普通函數(shù)參數(shù)和返回值的工具類型和獲取構(gòu)造函數(shù)參數(shù)和返回值的構(gòu)造類型。

定義:

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

/**
 * Obtain the parameters of a constructor function type in a tuple
 */
type ConstructorParameters any> = T extends new (...args: infer P) => any ? P : never;

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

/**
 * Obtain the return type of a constructor function type
 */
type InstanceType any> = T extends new (...args: any) => infer R ? R : any;

知識點:

infer:關(guān)鍵字,在extends條件類型語句(T extends U ?X : Y)中,允許在類型U的位置上使用關(guān)鍵字infer定義可推斷的類型變量,可推斷的類型變量只允許在類型X的位置上使用。簡單應(yīng)用如下,取出數(shù)組中的類型:

type ExtractArrayItemType = T extends (infer U)[] ? U : T;
// 條件判斷為 true,返回 U
type T = ExtractArrayItemType; // string

作用:

Parameters工具類型能夠獲取函數(shù)類型的參數(shù)類型,并使用參數(shù)類型構(gòu)造一個元組類型; ConstructorParameters工具類型可以把構(gòu)造函數(shù)的參數(shù)類型作為一個元組類型返回;ReturnType工具類型可以獲取函數(shù)的返回值類型; InstanceType工具類型可以獲取構(gòu)造函數(shù)的返回類型;

應(yīng)用:

type Fn = (a: string, b: number) => string;
type FnParamTypes = Parameters(Fn);  // [string, number]
type FnReturnType = ReturnType(Fn); // string

interface FunctionConstructor {
  new(...args: string[]): Function;
  (...args: string[]): Function;
  readonly prototype: Function;
}

type ConstructorParamTypes = ConstructorParameters(FunctionConstructor) // string[]

type ConstructorInstanceType = InstanceType(FunctionConstructor) // Function

ThisParameterType、OmitThisParameter與ThisType

該組類型均為與this相關(guān)的工具類型。

定義:

/**
 * Extracts the type of the 'this' parameter of a function type, or 'unknown' if the function type has no 'this' parameter.
 */
type ThisParameterType = T extends (this: infer U, ...args: any[]) => any ? U : unknown;

/**
 * Removes the 'this' parameter from a function type.
 */
type OmitThisParameter = unknown extends ThisParameterType ? T : T extends (...args: infer A) => infer R ? (...args: A) => R : T;
/**
 * Marker for contextual 'this' type
 */
interface ThisType { }

知識點:

unknown:頂端類型,TypeScript中僅有any和unknown兩種頂端類型,所有其他類型都可以賦值給兩者,但unknown只能賦值給any類型和unknown類型。TypeScript中只有一個尾端類型never,是其他所有類型的子類型。

作用 ThisParameterType類型可以獲取函數(shù)參數(shù)中this參數(shù)的類型;OmitThisParameter類型可以剔除函數(shù)參數(shù)中this參數(shù)的類型;ThisType類型可以對象字面量中this的類型。

應(yīng)用

interface Foo {
    x: number
};

function fn(this: Foo) {}

type Test = ThisParameterType; // Foo

type Fn = (this: Foo) => void

type NonReturnFn = OmitThisParameter; // () => void

let obj: ThisType<{x: number, getX: () => number}>

obj = {
  x: 100,
  getX(){
    return this.x
  }
}

以上簡單介紹了TypeScript中的自帶工具類型,TypeScript與JavaScript有相通之處,但又有更多的不同和背景知識,只有內(nèi)化了這些知識同時不斷地練習(xí)才能有效掌握這一門語言。


當(dāng)前文章:一看就懂的TypeScript工具類型
標(biāo)題鏈接:http://www.5511xx.com/article/djohgcs.html