내용으로 건너뛰기
GaramX
사용자 도구
로그인
사이트 도구
검색
도구
문서 보기
이전 판
역링크
최근 바뀜
미디어 관리자
사이트맵
로그인
>
최근 바뀜
미디어 관리자
사이트맵
현재 위치:
home
»
typescript
»
utility
추적:
•
comma
typescript:utility
이 문서는 읽기 전용입니다. 원본을 볼 수는 있지만 바꿀 수는 없습니다. 문제가 있다고 생각하면 관리자에게 문의하세요.
====== Utility Types ====== ===== 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. Example <code javascript> interface Todo { title: string; description: string; } 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', }); </code> ===== Readonly<T> ===== Constructs a type with all properties of T set to readonly, meaning the properties of the constructed type cannot be reassigned. Example # <code javascript> interface Todo { title: string; } const todo: Readonly<Todo> = { title: 'Delete inactive users', }; 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). ===== Object.freeze ===== function freeze<T>(obj: T): Readonly<T>; Record<K,T> # Constructs a type with a set of properties K of type T. This utility can be used to map the properties of a type to another type. Example # <code javascript> interface PageInfo { title: string; } type Page = 'home' | 'about' | 'contact'; const x: Record<Page, PageInfo> = { about: { title: 'about' }, contact: { title: 'contact' }, home: { title: 'home' }, }; </code> ===== Pick<T,K> ===== Constructs a type by picking the set of properties K from T. Example # <code javascript> interface Todo { title: string; description: string; completed: boolean; } type TodoPreview = Pick<Todo, 'title' | 'completed'>; const todo: TodoPreview = { title: 'Clean room', completed: false, }; </code> ===== Omit<T,K> ===== Constructs a type by picking all properties from T and then removing K. Example # <code javascript> interface Todo { title: string; description: string; completed: boolean; } type TodoPreview = Omit<Todo, 'description'>; const todo: TodoPreview = { title: 'Clean room', completed: false, }; </code> ===== Exclude<T,U> ===== Constructs a type by excluding from T all properties that are assignable to U. Example # <code javascript> type T0 = 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 </code> ===== Extract<T,U> ===== Constructs a type by extracting from T all properties that are assignable to U. Example # <code javascript> type T0 = Extract<"a" | "b" | "c", "a" | "f">; // "a" type T1 = Extract<string | number | (() => void), Function>; // () => void </code> ===== NonNullable<T> ===== Constructs a type by excluding null and undefined from T. Example # <code javascript> type T0 = NonNullable<string | number | undefined>; // string | number type T1 = NonNullable<string[] | null | undefined>; // string[] </code> ===== ReturnType<T> ===== Constructs a type consisting of the return type of function T. Example # <code javascript> type T0 = ReturnType<() => string>; // string type T1 = ReturnType<(s: string) => void>; // void type T2 = ReturnType<(<T>() => T)>; // {} type T3 = ReturnType<(<T extends U, U extends number[]>() => T)>; // number[] type T4 = ReturnType<typeof f1>; // { a: number, b: string } type T5 = ReturnType<any>; // any type T6 = ReturnType<never>; // any type T7 = ReturnType<string>; // Error type T8 = ReturnType<Function>; // Error </code> ===== InstanceType<T> ===== Constructs a type consisting of the instance type of a constructor function type T. Example # <code javascript> class C { x = 0; y = 0; } type T0 = InstanceType<typeof C>; // C type T1 = InstanceType<any>; // any type T2 = InstanceType<never>; // any type T3 = InstanceType<string>; // Error type T4 = InstanceType<Function>; // Error </code> ===== Required<T> ===== Constructs a type consisting of all properties of T set to required. Example # <code javascript> interface Props { a?: number; b?: string; }; const obj: Props = { a: 5 }; // OK const obj2: Required<Props> = { a: 5 }; // Error: property 'b' missing </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. Example # <code javascript> // Compile with --noImplicitThis type ObjectDescriptor<D, M> = { data?: D; methods?: M & ThisType<D & M>; // Type of 'this' in methods is D & M } function makeObject<D, M>(desc: ObjectDescriptor<D, M>): D & M { let data: object = desc.data || {}; let methods: object = desc.methods || {}; return { ...data, ...methods } as D & M; } let obj = makeObject({ data: { x: 0, y: 0 }, methods: { moveBy(dx: number, dy: number) { this.x += dx; // Strongly typed this this.y += dy; // Strongly typed this } } }); obj.x = 10; obj.y = 20; obj.moveBy(5, 5); </code> 위의 예제에서 makeObject 인수의 methods 객체는 thisType <D & M>을 포함하는 컨텍스트 유형을 가지므로 메서드 객체 내의 메소드에있는이 유형은 {x : number, y : number} 및 {moveBy (dx : number, dy : number) : number}. 메소드 프로퍼티의 타입이 추론 타겟인지, 메소드의이 타입에 대한 소스인지를 주목하십시오. ThisType<T> 마커 인터페이스는 lib.d.ts에 선언 된 빈 인터페이스입니다. 인터페이스는 객체 리터럴의 컨텍스트 유형에서 인식되는 것 이외에 빈 인터페이스처럼 작동합니다.
typescript/utility.txt
· 마지막으로 수정됨: 2025/04/15 10:05 저자
127.0.0.1
문서 도구
문서 보기
이전 판
역링크
맨 위로