문서의 선택한 두 판 사이의 차이를 보여줍니다.
| 다음 판 | 이전 판 | ||
| typescript:utility [2019/07/30 15:52] – 만듦 taekgu | typescript:utility [2025/04/15 10:05] (현재) – 바깥 편집 127.0.0.1 | ||
|---|---|---|---|
| 줄 1: | 줄 1: | ||
| ====== Utility Types ====== | ====== Utility Types ====== | ||
| - | ====== Partial< | + | ===== Partial< |
| Constructs a type with all properties of T set to optional. This utility will return a type that represents all subsets of a given type. | Constructs a type with all properties of T set to optional. This utility will return a type that represents all subsets of a given type. | ||
| - | Example | + | Example |
| + | <code javascript> | ||
| interface Todo { | interface Todo { | ||
| title: string; | title: string; | ||
| 줄 22: | 줄 23: | ||
| description: | description: | ||
| }); | }); | ||
| - | Readonly< | + | </ |
| + | ===== Readonly< | ||
| Constructs a type with all properties of T set to readonly, meaning the properties of the constructed type cannot be reassigned. | Constructs a type with all properties of T set to readonly, meaning the properties of the constructed type cannot be reassigned. | ||
| Example # | Example # | ||
| + | <code javascript> | ||
| interface Todo { | interface Todo { | ||
| title: string; | title: string; | ||
| 줄 35: | 줄 39: | ||
| todo.title = ' | todo.title = ' | ||
| + | </ | ||
| + | |||
| This utility is useful for representing assignment expressions that will fail at runtime (i.e. when attempting to reassign properties of a frozen object). | This utility is useful for representing assignment expressions that will fail at runtime (i.e. when attempting to reassign properties of a frozen object). | ||
| - | Object.freeze | + | ===== Object.freeze |
| function freeze< | function freeze< | ||
| Record< | Record< | ||
| 줄 43: | 줄 50: | ||
| Example # | Example # | ||
| + | <code javascript> | ||
| interface PageInfo { | interface PageInfo { | ||
| title: string; | title: string; | ||
| 줄 54: | 줄 62: | ||
| home: { title: ' | home: { title: ' | ||
| }; | }; | ||
| - | Pick< | + | </ |
| + | |||
| + | ===== Pick< | ||
| Constructs a type by picking the set of properties K from T. | Constructs a type by picking the set of properties K from T. | ||
| Example # | Example # | ||
| + | <code javascript> | ||
| interface Todo { | interface Todo { | ||
| title: string; | title: string; | ||
| 줄 70: | 줄 82: | ||
| completed: false, | completed: false, | ||
| }; | }; | ||
| - | Omit< | + | </ |
| + | ===== Omit< | ||
| Constructs a type by picking all properties from T and then removing K. | Constructs a type by picking all properties from T and then removing K. | ||
| Example # | Example # | ||
| + | <code javascript> | ||
| interface Todo { | interface Todo { | ||
| title: string; | title: string; | ||
| 줄 86: | 줄 101: | ||
| completed: false, | completed: false, | ||
| }; | }; | ||
| - | Exclude< | + | </ |
| + | ===== Exclude< | ||
| Constructs a type by excluding from T all properties that are assignable to U. | Constructs a type by excluding from T all properties that are assignable to U. | ||
| Example # | Example # | ||
| + | <code javascript> | ||
| type T0 = Exclude<" | type T0 = Exclude<" | ||
| type T1 = Exclude<" | type T1 = Exclude<" | ||
| type T2 = Exclude< | type T2 = Exclude< | ||
| - | Extract< | + | </ |
| + | |||
| + | ===== Extract< | ||
| Constructs a type by extracting from T all properties that are assignable to U. | Constructs a type by extracting from T all properties that are assignable to U. | ||
| Example # | Example # | ||
| + | <code javascript> | ||
| type T0 = Extract<" | type T0 = Extract<" | ||
| type T1 = Extract< | type T1 = Extract< | ||
| - | NonNullable< | + | </ |
| + | |||
| + | ===== NonNullable< | ||
| Constructs a type by excluding null and undefined from T. | Constructs a type by excluding null and undefined from T. | ||
| Example # | Example # | ||
| + | <code javascript> | ||
| type T0 = NonNullable< | type T0 = NonNullable< | ||
| type T1 = NonNullable< | type T1 = NonNullable< | ||
| - | ReturnType< | + | </ |
| + | |||
| + | ===== ReturnType< | ||
| Constructs a type consisting of the return type of function T. | Constructs a type consisting of the return type of function T. | ||
| Example # | Example # | ||
| + | <code javascript> | ||
| type T0 = ReturnType< | type T0 = ReturnType< | ||
| type T1 = ReturnType< | type T1 = ReturnType< | ||
| 줄 118: | 줄 148: | ||
| type T7 = ReturnType< | type T7 = ReturnType< | ||
| type T8 = ReturnType< | type T8 = ReturnType< | ||
| - | InstanceType< | + | </ |
| + | ===== InstanceType< | ||
| Constructs a type consisting of the instance type of a constructor function type T. | Constructs a type consisting of the instance type of a constructor function type T. | ||
| Example # | Example # | ||
| + | <code javascript> | ||
| class C { | class C { | ||
| x = 0; | x = 0; | ||
| 줄 132: | 줄 165: | ||
| type T3 = InstanceType< | type T3 = InstanceType< | ||
| type T4 = InstanceType< | type T4 = InstanceType< | ||
| - | Required< | + | </ |
| + | ===== Required< | ||
| Constructs a type consisting of all properties of T set to required. | Constructs a type consisting of all properties of T set to required. | ||
| Example # | Example # | ||
| + | <code javascript> | ||
| interface Props { | interface Props { | ||
| a?: number; | a?: number; | ||
| 줄 144: | 줄 180: | ||
| const obj2: Required< | const obj2: Required< | ||
| - | ThisType< | + | </ |
| + | ===== ThisType< | ||
| This utility does not return a transformed type. Instead, it serves a marker for a contextual this type. Note that the --noImplicitThis flag must be enabled to use this utility. | This utility does not return a transformed type. Instead, it serves a marker for a contextual this type. Note that the --noImplicitThis flag must be enabled to use this utility. | ||
| Example # | Example # | ||
| + | <code javascript> | ||
| // Compile with --noImplicitThis | // Compile with --noImplicitThis | ||
| 줄 174: | 줄 213: | ||
| obj.y = 20; | obj.y = 20; | ||
| obj.moveBy(5, | obj.moveBy(5, | ||
| - | In the example above, the methods object in the argument to makeObject | + | </ |
| + | |||
| + | 위의 예제에서 | ||
| - | The ThisType< | + | ThisType< |