사용자 도구

사이트 도구


typescript:utility

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

다음 판
이전 판
typescript:utility [2019/07/30 15:52] – 만듦 taekgutypescript:utility [2025/04/15 10:05] (현재) – 바깥 편집 127.0.0.1
줄 1: 줄 1:
 ====== Utility Types ====== ====== Utility Types ======
-====== Partial<T> # ======+===== Partial<T> =====
  
 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: 'throw out trash',     description: 'throw out trash',
 }); });
-Readonly<T> #+</code> 
 +===== Readonly<T> ===== 
 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 = 'Hello'; // Error: cannot reassign a readonly property todo.title = 'Hello'; // Error: cannot reassign a readonly property
 +</code>
 +
 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<T>(obj: T): Readonly<T>; function freeze<T>(obj: T): Readonly<T>;
 Record<K,T> # Record<K,T> #
줄 43: 줄 50:
  
 Example # Example #
 +<code javascript>
 interface PageInfo { interface PageInfo {
     title: string;     title: string;
줄 54: 줄 62:
     home: { title: 'home' },     home: { title: 'home' },
 }; };
-Pick<T,K> #+</code> 
 + 
 +===== Pick<T,K> ===== 
 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<T,K> #+</code> 
 +===== Omit<T,K> ===== 
 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<T,U> #+</code> 
 +===== Exclude<T,U> ===== 
 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<"a" | "b" | "c", "a">;  // "b" | "c" type T0 = Exclude<"a" | "b" | "c", "a">;  // "b" | "c"
 type T1 = Exclude<"a" | "b" | "c", "a" | "b">;  // "c" type T1 = Exclude<"a" | "b" | "c", "a" | "b">;  // "c"
 type T2 = Exclude<string | number | (() => void), Function>;  // string | number type T2 = Exclude<string | number | (() => void), Function>;  // string | number
-Extract<T,U> #+</code> 
 + 
 +===== Extract<T,U> ===== 
 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<"a" | "b" | "c", "a" | "f">;  // "a" type T0 = Extract<"a" | "b" | "c", "a" | "f">;  // "a"
 type T1 = Extract<string | number | (() => void), Function>;  // () => void type T1 = Extract<string | number | (() => void), Function>;  // () => void
-NonNullable<T> #+</code> 
 + 
 +===== NonNullable<T> ===== 
 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<string | number | undefined>;  // string | number type T0 = NonNullable<string | number | undefined>;  // string | number
 type T1 = NonNullable<string[] | null | undefined>;  // string[] type T1 = NonNullable<string[] | null | undefined>;  // string[]
-ReturnType<T> #+</code> 
 + 
 +===== ReturnType<T> ===== 
 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<() => string>;  // string type T0 = ReturnType<() => string>;  // string
 type T1 = ReturnType<(s: string) => void>;  // void type T1 = ReturnType<(s: string) => void>;  // void
줄 118: 줄 148:
 type T7 = ReturnType<string>;  // Error type T7 = ReturnType<string>;  // Error
 type T8 = ReturnType<Function>;  // Error type T8 = ReturnType<Function>;  // Error
-InstanceType<T> #+</code> 
 +===== InstanceType<T> ===== 
 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<string>;  // Error type T3 = InstanceType<string>;  // Error
 type T4 = InstanceType<Function>;  // Error type T4 = InstanceType<Function>;  // Error
-Required<T> #+</code> 
 +===== Required<T> ===== 
 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<Props> = { a: 5 }; // Error: property 'b' missing const obj2: Required<Props> = { a: 5 }; // Error: property 'b' missing
-ThisType<T> #+</code> 
 +===== ThisType<T> ===== 
 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, 5); obj.moveBy(5, 5);
-In the example above, the methods object in the argument to makeObject has a contextual type that includes ThisType<D & M> and therefore the type of this in methods within the methods object is { x: number, y: number } { moveBy(dx: number, dy: number): number }. Notice how the type of the methods property simultaneously is an inference target and a source for the this type in methods.+</code> 
 + 
 +위의 예제에서 makeObject 인수의 methods 객체는 thisType <D & M>을 포함하는 컨텍스트 유형을 가지므로 메서드 객체 내의 메소드에있는이 유형은 {x : number, y : number} 및 {moveBy (dx : number, dy : number) : number}. 메소드 프로퍼티의 타입이 추론 타겟인지, 메소드의이 타입에 대한 소스인지를 주목하십시오.
  
-The ThisType<T> marker interface is simply an empty interface declared in lib.d.ts. Beyond being recognized in the contextual type of an object literal, the interface acts like any empty interface.+ThisType<T> 마커 인터페이스는 lib.d.ts에 선언 된 빈 인터페이스입니다인터페이스는 객체 리터럴의 컨텍스트 유형에서 인식되는 것 이외에 빈 인터페이스처럼 작동합니다.
typescript/utility.1564501933.txt.gz · 마지막으로 수정됨: 2025/04/15 10:05 (바깥 편집)