is
进行类型收窄function isUser(person: Person): person is User {
return person.type === 'user'
}
&
进行属性的合并使用&
操作符可以实现属性合并
type User = {
name: string
}
type Admin = {
age: number
}
type Person = User | Admin;
// 此时 Person 可以包含 name 或者可以包含 age
type Staff = User & Admin;
// 此时 Staff 必须同时包含 name 和age
Partial
使用Partial
将所有类型属性变成可选
interface Todo {
title: string;
description: string;
}
function updateTodo(todo: Todo, fieldsToUpdate: Partial) {
return { ...todo, ...fieldsToUpdate };
}
const todo1 = {
title: "organize desk",
description: "clear clutter",
};
const todo2 = updateTodo(todo1, {
description: "throw out trash",
});
Partial
的实现:
type Partial = { [P in keyof T]?: T[P] };
Required
与Partial
相反,将可选属性变为必选
interface Props {
a?: number;
b?: string;
}
const obj: Props = { a: 5 };
const obj2: Required = { a: 5 };
// 报错
// Property 'b' is missing in type '{ a: number; }' but required in type 'Required'.
Readonly
使用Readonly
将类型设置为只读
interface Todo {
title: string;
}
const todo: Readonly = {
title: "Delete inactive users",
};
todo.title = "Hello";
// 报错
// Cannot assign to 'title' because it is a read-only property.
Record
使用Record
生成的类型,包含keys
对应的属性键名,键值为Type
,常用来实现一种类型对另外一种类型的映射关系:
interface PageInfo {
title: string;
}
type Page = 'home' | 'about' | 'concat'';
const nav: Record = {
about: {title: 'about'},
contact: {title: 'contact'},
home: {title: 'home'},
}
Pick
使用Pick
来在Type
中拾取keys
对应的属性
interface Todo {
title: string;
description: string;
completed: boolean;
}
type TodoPreview = Pick;
const todo: TodoPreview = {
title: 'Clean Room',
completed: false
}
Omit
从Type
中生成所有属性,并且移除key
对应的属性
interface Todo {
title: string;
description: string;
completed: boolean;
}
type TodoPrview = Omit;
const todo: TodoPreview = {
title: "Clean room",
completed: false,
};
Exclude
在Type
中移除ExcludedUnion
中的所有类型(差集)
type T0 = Exclude<"a" | "b" | "c", "a">;
// type T0 = "b" | "c"
type T1 = Exclude<"a" | "b" | "c", "a" | "b">;
// type T1 = "c"
type T2 = Exclude void), Function>;
// type T2 = string | number
Extract
生成Type
与Union
的交集
type T0 = Extract<"a" | "b" | "c", "a" | "f">;
// type T0 = "a"
type T1 = Extract void), Function>;
// type T1 = () => void
NonNullable
生成不为null
和undefined
的类型
type T0 = NonNullable;
// type T0 = string | number
type T1 = NonNullable;
// type T1 = string[]
Parameters
生成对应的函数类型的Type的参数元组类型
declare function f1(arg: { a: number; b: string }): void;
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]
type T3 = Parameters;
// type T3 = [arg: {
// a: number;
// b: string;
// }]
ReturnType
生成函数返回值对应的类型
declare function f1(): { a: number; b: string };
type T0 = ReturnType<() => string>;
// type T0 = string
type T1 = ReturnType<(s: string) => void>;
// type T1 = void
type T2 = ReturnType<() => T>;
// type T2 = unknown
type T4 = ReturnType;
// type T4 = {
// a: number;
// b: string;
// }
ThisParameterType
对于一个函数的this
的类型,可以通过在参数中对进行声明:
function toHex(this: Number) {
return this.toString(16);
}
这时可以使用ThisParameterType
返回this
类型,如果函数没有声明this
类型,那么会返回unknown
function numberToString(n: ThisParameterType) {
return toHex.apply(n);
}