0%

实用程序类型

Partial

构造一个所有属性的Type都设置为可选的类型,返回一个表示给定类型的所有子集的类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
interface Todo {
title:string;
description:stirng;
}
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",
});

Required

构造一个由所有属性类型都是required的类型,与Partial相反

1
2
3
4
5
6
7
8
9
interface Props {
a?: number;
b?: string;
}

const obj: Props = { a: 5 };

const obj2: Required<Props> = { a: 5 };
//Property 'b' is missing in type '{ a: number; }' but required in type 'Required<Props>'.

Readonly

构造一个所有属性的Type都设置为readonly的类型,这意味着构造类型的属性不能重新分配

1
2
3
4
5
6
7
8
9
10
interface Todo {
title: string;
}

const todo: Readonly<Todo> = {
title: "Delete inactive users",
};

todo.title = "Hello";
//Cannot assign to 'title' because it is a read-only property.

Record<Keys,Type>

构造一个对象类型,其属性键为keys,属性值为Type,可用于将一种类型的属性映射到另一种类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
interface CatInfo {
age: number;
breed: string;
}

type CatName = "miffy" | "boris" | "mordred";

const cats: Record<CatName, CatInfo> = {
miffy: { age: 10, breed: "Persian" },
boris: { age: 5, breed: "Maine Coon" },
mordred: { age: 16, breed: "British Shorthair" },
};

cats.boris;

//const cats: Record<CatName, CatInfo

Pick<Type,Keys>

通过从中选择一组属性keys来构造类型Type

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
interface Todo {
title: string;
description: string;
completed: boolean;
}

type TodoPreview = Pick<Todo, "title" | "completed">;

const todo: TodoPreview = {
title: "Clean room",
completed: false,
};

todo;

//const todo: TodoPreview

omit<Type,Keys>

Type通过从中选择所有属性然后删除keys来构造类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
interface Todo {
title: string;
description: string;
completed: boolean;
createdAt: number;
}

type TodoPreview = Omit<Todo, "description">;

const todo: TodoPreview = {
title: "Clean room",
completed: false,
createdAt: 1615544252770,
};

todo;

//const todo: TodoPreview

type TodoInfo = Omit<Todo, "completed" | "createdAt">;

const todoInfo: TodoInfo = {
title: "Pick up kids",
description: "Kindergarten closes at 5pm",
};

todoInfo;

//const todoInfo: TodoInfo

Exclude<UnionType,ExcludedMembers>

通过从UnionType中排除可以赋值给ExcludedMembers的值

1
2
3
4
5
6
7
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<string | number | (() => void), Function>;

//type T2 = string | number

Extract<Type,Union>

提取出Type和Union的交集(可以赋值给Union成员的值)

1
2
3
4
5
6
type T0 = Extract<"a" | "b" | "c", "a" | "f">;

//type T0 = "a"
type T1 = Extract<string | number | (() => void), Function>;

//type T1 = () => void

NonNullable

构造一个类型,可以从Type中排除null和undefined

1
2
3
4
5
6
type T0 = NonNullable<string | number | undefined>;

//type T0 = string | number
type T1 = NonNullable<string[] | null | undefined>;

//type T1 = string[]

ReturnType

由函数返回类型组成(Type必须是一个(…args:any)=>any)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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>() => T>;

//type T2 = unknown
type T3 = ReturnType<<T extends U, U extends number[]>() => T>;

//type T3 = number[]
type T4 = ReturnType<typeof f1>;

//type T4 = {
// a: number;
// b: string;
//}
type T5 = ReturnType<any>;

//type T5 = any
type T6 = ReturnType<never>;

//type T6 = never
type T7 = ReturnType<string>;
//Type 'string' does not satisfy the constraint '(...args: any) => any'.

//type T7 = any
type T8 = ReturnType<Function>;
//Type 'Function' does not satisfy the constraint '(...args: any) => any'.
// Type 'Function' provides no match for the signature '(...args: any): any'.

//type T8 = any

官网链接:https://www.typescriptlang.org/docs/handbook/utility-types.html#returntypetype