Skip to content

Commit

Permalink
docs(Extract): Add Extract documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
haejunejung committed Aug 14, 2024
1 parent df6748f commit af885b9
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 2 deletions.
78 changes: 77 additions & 1 deletion docs/ko/reference/built-in/Extract.md
Original file line number Diff line number Diff line change
@@ -1 +1,77 @@
# Extract
# Record\<Type, Union>

:::info
`Extract<Type, Union>`은 TypeScript 2.8 이상에서 사용할 수 있는 내장 유틸리티 타입이에요. 자세한 내용은 [타입스크립트 핸드북](https://www.typescriptlang.org/docs/handbook/utility-types.html#extracttype-union)[릴리즈 노트](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#predefined-conditional-types)를 참고해주세요.
:::

## 개요

`Extract<Type, Union>``Union`에 할당할 수 있는 모든 유니온 멤버를 `Type`에서 추출하여 타입을 구성하는 TypeScript 내장된 유틸리티 타입이에요.

## 문법

```ts
type Extract<T, U> = T extends U ? T : never;
```

- **Type (T)**: 유니온 멤버를 추출하고자 하는 타입을 나타내요.
- **Union (U)**: `Type`에서 추출하고자 하는 타입을 나타내요.

## 예제

#### 예제 #1

```ts
type User = 'admin' | 'editor' | 'viewer';

type PrivilegedUser = Extract<User, 'admin' | 'editor'>; // 'admin' | 'editor'
```

#### 예제 #2

```ts
type OnlyFunction = Extract<string | number | symbol | (() => void), Function>; // () => void
```

#### 예제 #3

```ts
type Primitive = string | number | boolean | bigint | symbol | null | undefined;

type NonPrimitive<T> = Extract<T, Primitive>;

type NonPrimitiveTypes = NonPrimitive<string | number | object>; // object
```

#### 예제 #4

```ts
declare function uniqueId(): number;

const ID = Symbol('ID');

interface Product {
[ID]: number;
name: string;
price: number;
}

type ProductUpdatableKeys = Extract<keyof Product, string>;

function updateProduct <
Obj extends Product,
Key extends ProductUpdatableKeys,
Value extends Obj[Key]
> (obj: Obj, key: Key, value: Value) {
obj[key] = value;
}

const product: Product = {
[ID]: uniqueId(),
name: 'box',
price: 5000
}

updateProduct(product, 'name', 'paper'); //
updateProduct(product ID, uniqueId()); //
```
78 changes: 77 additions & 1 deletion docs/reference/built-in/Extract.md
Original file line number Diff line number Diff line change
@@ -1 +1,77 @@
# Extract
# Extract\<Type, Union>

:::info
The `Extract<Type, Union>` utility type is available starting from TypeScript version 2.8. For more information see in [TypeScript Handbook](https://www.typescriptlang.org/docs/handbook/utility-types.html#extracttype-union), [Release Note](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#predefined-conditional-types).
:::

## Overview

The `Extract<Type, Union>` is a built in utility type in TypeScript that constructs a type by extracting from `Type` all union members that are assignable to `Union`.

## Syntax

```ts
type Extract<T, U> = T extends U ? T : never;
```

- **Type (T)**: This represents a type from which you want to extract union members.
- **Union (U)**: This represents the members or types that you want to extract from the `Type`.

## Examples

#### Example #1

```ts
type User = 'admin' | 'editor' | 'viewer';

type PrivilegedUser = Extract<User, 'admin' | 'editor'>; // 'admin' | 'editor'
```

#### Example #2

```ts
type OnlyFunction = Extract<string | number | symbol | (() => void), Function>; // () => void
```

#### Example #3

```ts
type Primitive = string | number | boolean | bigint | symbol | null | undefined;

type NonPrimitive<T> = Extract<T, Primitive>;

type NonPrimitiveTypes = NonPrimitive<string | number | object>; // object
```

#### Example #4

```ts
declare function uniqueId(): number;

const ID = Symbol('ID');

interface Product {
[ID]: number;
name: string;
price: number;
}

type ProductUpdatableKeys = Extract<keyof Product, string>;

function updateProduct <
Obj extends Product,
Key extends ProductUpdatableKeys,
Value extends Obj[Key]
> (obj: Obj, key: Key, value: Value) {
obj[key] = value;
}

const product: Product = {
[ID]: uniqueId(),
name: 'box',
price: 5000
}

updateProduct(product, 'name', 'paper'); //
updateProduct(product ID, uniqueId()); //
```

0 comments on commit af885b9

Please sign in to comment.