Partial<Type>
構造類型Type
,並將它全部的屬性設置爲可選的。它的返回類型表示輸入類型的全部子類型。typescript
interface Todo { title: string; description: string; } function updateTodo(todo: Todo, fieldsToUpdate: Partial<Todo>) { return { ...todo, ...fieldsToUpdate }; } const todo1 = { title: 'organize desk', description: 'clear clutter', }; const todo2 = updateTodo(todo1, { description: 'throw out trash', });
Readonly<Type>
構造類型Type
,並將它全部的屬性設置爲readonly
,也就是說構造出的類型的屬性不能被再次賦值。數組
interface Todo { title: string; } const todo: Readonly<Todo> = { title: 'Delete inactive users', }; todo.title = 'Hello'; // Error: cannot reassign a readonly property
這個工具可用來表示在運行時會失敗的賦值表達式(好比,當嘗試給凍結對象的屬性再次賦值時)。app
Object.freeze
function freeze<T>(obj: T): Readonly<T>;
Record<Keys, Type>
構造一個類型,其屬性名的類型爲K
,屬性值的類型爲T
。這個工具可用來將某個類型的屬性映射到另外一個類型上。函數
interface PageInfo { title: string; } type Page = 'home' | 'about' | 'contact'; const x: Record<Page, PageInfo> = { about: { title: 'about' }, contact: { title: 'contact' }, home: { title: 'home' }, };
Pick<Type, Keys>
從類型Type
中挑選部分屬性Keys
來構造類型。工具
interface Todo { title: string; description: string; completed: boolean; } type TodoPreview = Pick<Todo, 'title' | 'completed'>; const todo: TodoPreview = { title: 'Clean room', completed: false, };
Omit<Type, Keys>
從類型Type
中獲取全部屬性,而後從中剔除Keys
屬性後構造一個類型。ui
interface Todo { title: string; description: string; completed: boolean; } type TodoPreview = Omit<Todo, 'description'>; const todo: TodoPreview = { title: 'Clean room', completed: false, };
Exclude<Type, ExcludedUnion>
從類型Type
中剔除全部能夠賦值給ExcludedUnion
的屬性,而後構造一個類型。this
type T0 = Exclude<'a' | 'b' | 'c', 'a'>; // "b" | "c" type T1 = Exclude<'a' | 'b' | 'c', 'a' | 'b'>; // "c" type T2 = Exclude<string | number | (() => void), Function>; // string | number
Extract<Type, Union>
從類型Type
中提取全部能夠賦值給Union
的類型,而後構造一個類型。code
type T0 = Extract<'a' | 'b' | 'c', 'a' | 'f'>; // "a" type T1 = Extract<string | number | (() => void), Function>; // () => void
NonNullable<Type>
從類型Type
中剔除null
和undefined
,而後構造一個類型。對象
type T0 = NonNullable<string | number | undefined>; // string | number type T1 = NonNullable<string[] | null | undefined>; // string[]
Parameters<Type>
由函數類型Type
的參數類型來構建出一個元組類型。接口
declare function f1(arg: { a: number; b: string }): void; type T0 = Parameters<() => string>; // [] type T1 = Parameters<(s: string) => void>; // [s: string] type T2 = Parameters<<T>(arg: T) => T>; // [arg: unknown] type T3 = Parameters<typeof f1>; // [arg: { a: number; b: string; }] type T4 = Parameters<any>; // unknown[] type T5 = Parameters<never>; // never type T6 = Parameters<string>; // never // Type 'string' does not satisfy the constraint '(...args: any) => any'. type T7 = Parameters<Function>; // never // Type 'Function' does not satisfy the constraint '(...args: any) => any'.
ConstructorParameters<Type>
由構造函數類型來構建出一個元組類型或數組類型。
由構造函數類型Type
的參數類型來構建出一個元組類型。(若Type
不是構造函數類型,則返回never
)。
type T0 = ConstructorParameters<ErrorConstructor>; // [message?: string | undefined] type T1 = ConstructorParameters<FunctionConstructor>; // string[] type T2 = ConstructorParameters<RegExpConstructor>; // [pattern: string | RegExp, flags?: string | undefined] type T3 = ConstructorParameters<any>; // unknown[] type T4 = ConstructorParameters<Function>; // never // Type 'Function' does not satisfy the constraint 'new (...args: any) => any'.
ReturnType<Type>
由函數類型Type
的返回值類型構建一個新類型。
type T0 = ReturnType<() => string>; // string type T1 = ReturnType<(s: string) => void>; // void type T2 = ReturnType<(<T>() => T)>; // {} type T3 = ReturnType<(<T extends U, U extends number[]>() => T)>; // number[] type T4 = ReturnType<typeof f1>; // { a: number, b: string } type T5 = ReturnType<any>; // any type T6 = ReturnType<never>; // any type T7 = ReturnType<string>; // Error type T8 = ReturnType<Function>; // Error
InstanceType<Type>
由構造函數類型Type
的實例類型來構建一個新類型。
class C { x = 0; y = 0; } type T0 = InstanceType<typeof C>; // C type T1 = InstanceType<any>; // any type T2 = InstanceType<never>; // any type T3 = InstanceType<string>; // Error type T4 = InstanceType<Function>; // Error
Required<Type>
構建一個類型,使類型Type
的全部屬性爲required
。
與此相反的是Partial
。
interface Props { a?: number; b?: string; } const obj: Props = { a: 5 }; // OK const obj2: Required<Props> = { a: 5 }; // Error: property 'b' missing
ThisParameterType<Type>
從函數類型中提取 this 參數的類型。
若函數類型不包含 this
參數,則返回 unknown 類型。
function toHex(this: Number) { return this.toString(16); } function numberToString(n: ThisParameterType<typeof toHex>) { return toHex.apply(n); }
OmitThisParameter<Type>
從Type
類型中剔除 this
參數。
若未聲明 this
參數,則結果類型爲 Type
。
不然,由Type
類型來構建一個不帶this
參數的類型。
泛型會被忽略,而且只有最後的重載簽名會被採用。
function toHex(this: Number) { return this.toString(16); } const fiveToHex: OmitThisParameter<typeof toHex> = toHex.bind(5); console.log(fiveToHex());
ThisType<Type>
這個工具不會返回一個轉換後的類型。
它做爲上下文的this
類型的一個標記。
注意,若想使用此類型,必須啓用--noImplicitThis
。
// Compile with --noImplicitThis type ObjectDescriptor<D, M> = { data?: D; methods?: M & ThisType<D & M>; // Type of 'this' in methods is D & M }; function makeObject<D, M>(desc: ObjectDescriptor<D, M>): D & M { let data: object = desc.data || {}; let methods: object = desc.methods || {}; return { ...data, ...methods } as D & M; } let obj = makeObject({ data: { x: 0, y: 0 }, methods: { moveBy(dx: number, dy: number) { this.x += dx; // Strongly typed this this.y += dy; // Strongly typed this }, }, }); obj.x = 10; obj.y = 20; obj.moveBy(5, 5);
上面例子中,makeObject
參數裏的methods
對象具備一個上下文類型ThisType<D & M>
,所以methods
對象的方法裏this
的類型爲{ x: number, y: number } & { moveBy(dx: number, dy: number): number }
。
在lib.d.ts
裏,ThisType<T>
標識接口是個簡單的空接口聲明。除了在被識別爲對象字面量的上下文類型以外,這個接口與通常的空接口沒有什麼不一樣。