Skip to content

Commit

Permalink
update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
hojas committed Dec 19, 2023
1 parent ef53c04 commit fa2f8bf
Showing 1 changed file with 152 additions and 14 deletions.
166 changes: 152 additions & 14 deletions src/docs/typescript/utility-types.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
title: TypeScript 内置工具类型
title: TypeScript 内置工具类型的源码实现
---

# TypeScript 内置工具类型
# TypeScript 内置工具类型的源码实现

## Awaited\<Type>

Expand Down Expand Up @@ -56,10 +56,10 @@ interface Todo {
type PartialTodo = Partial<Todo>

// 等价于
// type PartialTodo = {
// title?: string
// description?: string
// }
type PartialTodo = {
title?: string
description?: string
}
```
源码实现:
Expand All @@ -80,13 +80,13 @@ interface Props {
b?: string
}

type RequiredProps = Required<Props>
type RequiredProps = Required<Props>

// 等价于
// type RequiredProps = {
// a: number
// b: string
// }
type RequiredProps = {
a: number
b: string
}
```
源码实现:
Expand All @@ -110,9 +110,9 @@ interface Todo {
type ReadonlyTodo = Readonly<Todo>

// 等价于
// type ReadonlyTodo = {
// readonly title: string
// }
type ReadonlyTodo = {
readonly title: string
}
```
源码实现:
Expand Down Expand Up @@ -150,6 +150,144 @@ type Record<K extends keyof any, T> = {
}
```
## Pick<Type, Keys>
从 Type 中选取属性 Keys(字符串字面量或字符串字面量的组合)集,构建一个类型。
```ts
interface Todo {
title: string
description: string
completed: boolean
}

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

// 等价于
interface TodoPreview {
title: string
completed: boolean
}
```

源码实现:

```ts
type Pick<T, K extends keyof T> = {
[P in K]: T[P]
}
```
## Omit<Type, Keys>
从 Type 中选取所有属性,然后删除键(字符串字面量或字符串字面量的联合),从而构造一个类型。与 Pick 相反。
```ts

interface Todo {
title: string
description: string
completed: boolean
createdAt: number
}

type TodoPreview = Omit<Todo, "description">

// 等价于
interface TodoPreview {
title: string
completed: boolean
createdAt: number
}
```

源码实现:

```ts
type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>
```
## Exclude<UnionType, ExcludedMembers>
通过从 UnionType 中排除所有可赋值给 ExcludedMembers 的联合成员来构造一个类型。
```ts
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

type Shape =
| { kind: "circle"; radius: number }
| { kind: "square"; x: number }
| { kind: "triangle"; x: number; y: number }
type T3 = Exclude<Shape, { kind: "circle" }>
// type T3 = {
// kind: "square"
// x: number
// } | {
// kind: "triangle"
// x: number
// y: number
// }
```
源码实现:
```ts
type Exclude<T, U> = T extends U ? never : T
```
## Extract<Type, Union>
从 Type 中提取可赋值给 Union 的所有 union 成员,从而构造一个类型。
```ts
type T0 = Extract<"a" | "b" | "c", "a" | "f">
// type T0 = "a"

type T1 = Extract<string | number | (() => void), Function>
// type T1 = () => void

type Shape =
| { kind: "circle"; radius: number }
| { kind: "square"; x: number }
| { kind: "triangle"; x: number; y: number }
type T2 = Extract<Shape, { kind: "circle" }>
// type T2 = {
// kind: "circle";
// radius: number;
// }
```
源码实现:
```ts
type Extract<T, U> = T extends U ? T : never
```
## NonNullable\<Type>
通过从 Type 中排除 null 和 undefined 来构造一个类型。
```ts
type T0 = NonNullable<string | number | undefined>
// type T0 = string | number

type T1 = NonNullable<string[] | null | undefined>
// type T1 = string[]
```
源码实现:
```ts
type NonNullable<T> = T extends null | undefined ? never : T
```
## 参考
https://www.typescriptlang.org/docs/handbook/utility-types.html

0 comments on commit fa2f8bf

Please sign in to comment.