TypeScriptλ κ³΅ν΅ νμ λ³νμ μ©μ΄νκ² νκΈ° μν΄ λͺ κ°μ§ μ νΈλ¦¬ν° νμ μ μ 곡ν©λλ€. μ΄λ° μ νΈλ¦¬ν°λ€μ μ μμΌλ‘ μ¬μ© κ°λ₯ν©λλ€.
Partial<T>
Readonly<T>
Record<K,T>
Pick<T,K>
Omit<T,K>
Exclude<T,U>
Extract<T,U>
NonNullable<T>
Parameters<T>
ConstructorParameters<T>
ReturnType<T>
InstanceType<T>
Required<T>
ThisParameterType
OmitThisParameter
ThisType<T>
T
μ λͺ¨λ νλ‘νΌν°λ₯Ό μ νμ μΌλ‘ λ§λλ νμ
μ ꡬμ±ν©λλ€. μ΄ μ νΈλ¦¬ν°λ μ£Όμ΄μ§ νμ
μ λͺ¨λ νμ νμ
μ§ν©μ λνλ΄λ νμ
μ λ°νν©λλ€.
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',
});
T
μ λͺ¨λ νλ‘νΌν°λ₯Ό μ½κΈ° μ μ©(readonly)
μΌλ‘ μ€μ ν νμ
μ ꡬμ±ν©λλ€, μ¦ μμ±λ νμ
μ νλ‘νΌν°λ μ¬ν λΉν μ μμ΅λλ€.
interface Todo {
title: string;
}
const todo: Readonly<Todo> = {
title: 'Delete inactive users',
};
todo.title = 'Hello'; // μ€λ₯: μ½κΈ° μ μ© νλ‘νΌν°μ μ¬ν λΉν μ μμ
μ΄ μ νΈλ¦¬ν°λ λ°νμμ μ€ν¨ν ν λΉ ννμμ λνλΌ λ μ μ©ν©λλ€.(μ, frozen κ°μ²΄μ νλ‘νΌν°μ μ¬ν λΉ νλ €κ³ νλ κ²½μ°)
function freeze<T>(obj: T): Readonly<T>;
νμ
T
μ νλ‘νΌν°μ μ§ν© K
λ‘ νμ
μ ꡬμ±ν©λλ€. μ΄ μ νΈλ¦¬ν°λ νμ
μ νλ‘νΌν°λ€μ λ€λ₯Έ νμ
μ 맀νμν€λ λ° μ¬μ©λ μ μμ΅λλ€.
interface PageInfo {
title: string;
}
type Page = 'home' | 'about' | 'contact';
const x: Record<Page, PageInfo> = {
about: { title: 'about' },
contact: { title: 'contact' },
home: { title: 'home' },
};
T
μμ νλ‘νΌν° K
μ μ§ν©μ μ νν΄ νμ
μ ꡬμ±ν©λλ€.
interface Todo {
title: string;
description: string;
completed: boolean;
}
type TodoPreview = Pick<Todo, 'title' | 'completed'>;
const todo: TodoPreview = {
title: 'Clean room',
completed: false,
};
T
μμ λͺ¨λ νλ‘νΌν°λ₯Ό μ νν λ€μ K
λ₯Ό μ κ±°ν νμ
μ ꡬμ±ν©λλ€.
interface Todo {
title: string;
description: string;
completed: boolean;
}
type TodoPreview = Omit<Todo, 'description'>;
const todo: TodoPreview = {
title: 'Clean room',
completed: false,
};
T
μμ U
μ ν λΉν μ μλ λͺ¨λ μμ±μ μ μΈν νμ
μ ꡬμ±ν©λλ€.
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
T
μμ U
μ ν λΉ ν μ μλ λͺ¨λ μμ±μ μΆμΆνμ¬ νμ
μ ꡬμ±ν©λλ€.
type T0 = Extract<"a" | "b" | "c", "a" | "f">; // "a"
type T1 = Extract<string | number | (() => void), Function>; // () => void
T
μμ null
κ³Ό undefined
λ₯Ό μ μΈν νμ
μ ꡬμ±ν©λλ€.
type T0 = NonNullable<string | number | undefined>; // string | number
type T1 = NonNullable<string[] | null | undefined>; // string[]
ν¨μ νμ
T
μ 맀κ°λ³μ νμ
λ€μ νν νμ
μ ꡬμ±ν©λλ€.
declare function f1(arg: { a: number, b: string }): void
type T0 = Parameters<() => string>; // []
type T1 = Parameters<(s: string) => void>; // [string]
type T2 = Parameters<(<T>(arg: T) => T)>; // [unknown]
type T4 = Parameters<typeof f1>; // [{ a: number, b: string }]
type T5 = Parameters<any>; // unknown[]
type T6 = Parameters<never>; // never
type T7 = Parameters<string>; // μ€λ₯
type T8 = Parameters<Function>; // μ€λ₯
ConstructorParameters<T>
νμ
μ μμ±μ ν¨μ νμ
μ λͺ¨λ 맀κ°λ³μ νμ
μ μΆμΆν μ μκ² ν΄μ€λλ€. λͺ¨λ 맀κ°λ³μ νμ
μ κ°μ§λ νν νμ
(T
κ° ν¨μκ° μλ κ²½μ° never
)μ μμ±ν©λλ€.
type T0 = ConstructorParameters<ErrorConstructor>; // [(string | undefined)?]
type T1 = ConstructorParameters<FunctionConstructor>; // string[]
type T2 = ConstructorParameters<RegExpConstructor>; // [string, (string | undefined)?]
ν¨μ T
μ λ°ν νμ
μΌλ‘ ꡬμ±λ νμ
μ λ§λλλ€.
declare function f1(): { a: number, b: string }
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>; // μ€λ₯
type T8 = ReturnType<Function>; // μ€λ₯
μμ±μ ν¨μ νμ
T
μ μΈμ€ν΄μ€ νμ
μΌλ‘ ꡬμ±λ νμ
μ λ§λλλ€.
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>; // μ€λ₯
type T4 = InstanceType<Function>; // μ€λ₯
T
μ λͺ¨λ νλ‘νΌν°κ° νμλ‘ μ€μ λ νμ
μ ꡬμ±ν©λλ€.
interface Props {
a?: number;
b?: string;
};
const obj: Props = { a: 5 }; // μ±κ³΅
const obj2: Required<Props> = { a: 5 }; // μ€λ₯: νλ‘νΌν° 'b'κ° μμ΅λλ€
ν¨μ νμ
μ this
맀κ°λ³μμ νμ
, νΉμ ν¨μ νμ
μ this
맀κ°λ³μκ° μμ κ²½μ° unknown
μ μΆμΆν©λλ€.
μ μ: μ΄ νμ
μ --strictFunctionTypes
κ° νμ±νλμμ λλ§ μ¬λ°λ₯΄κ² λμν©λλ€. #32964λ₯Ό μ°Έκ³ νμΈμ.
function toHex(this: Number) {
return this.toString(16);
}
function numberToString(n: ThisParameterType<typeof toHex>) {
return toHex.apply(n);
}
ν¨μ νμ μμ 'this' 맀κ°λ³μλ₯Ό μ κ±°ν©λλ€.
μ μ: μ΄ νμ
μ --strictFunctionTypes
κ° νμ±νλμμ λλ§ μ¬λ°λ₯΄κ² λμν©λλ€. #32964λ₯Ό μ°Έκ³ νμΈμ.
function toHex(this: Number) {
return this.toString(16);
}
// `bind`μ λ°ν νμ
μ μ΄λ―Έ `OmitThisParameter`μ μ¬μ©νκ³ μμ΅λλ€, μ΄λ λ¨μ§ μμ λ₯Ό μν κ²μ
λλ€.
const fiveToHex: OmitThisParameter<typeof toHex> = toHex.bind(5);
console.log(fiveToHex());
μ΄ μ νΈλ¦¬ν°λ λ³νλ νμ
μ λ°ννμ§ μμ΅λλ€. λμ , λ¬Έλ§₯μ this
νμ
μ νμνλ μν μ ν©λλ€. μ΄ μ νΈλ¦¬ν°λ₯Ό μ¬μ©νκΈ° μν΄μ --noImplicitThis
νλκ·Έλ₯Ό μ¬μ©ν΄μΌ νλ€λ κ²μ μ μνμΈμ.
// --noImplicitThis λ‘ μ»΄νμΌ
type ObjectDescriptor<D, M> = {
data?: D;
methods?: M & ThisType<D & M>; // λ©μλ μμ 'this νμ
μ 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; // κ°νκ² νμ
μ΄ μ ν΄μ§ this
this.y += dy; // κ°νκ² νμ
μ΄ μ ν΄μ§ this
}
}
});
obj.x = 10;
obj.y = 20;
obj.moveBy(5, 5);
μ μμ μμ, makeObject
μ μΈμλ‘ λ겨μ§λ methods
κ°μ²΄λ ThisType<D & M>
λ₯Ό ν¬ν¨ν λ¬Έλ§₯μ νμ
μ κ°μ§κ³ μκ³ , λ°λΌμ methods
κ°μ²΄μ λ©μλ μμ this
νμ
μ { x: number, y: number } & { moveBy(dx: number, dy: number): number }
μ
λλ€. method
νλ‘νΌν°μ νμ
μ΄ μΆλ‘ μ λμμ΄λ©° λμμ λ©μλ μμ this
νμ
μ μΆμ²μΈ κ²μ μ£Όλͺ©νμΈμ.
ThisType<T>
λ§μ»€ μΈν°νμ΄μ€λ λ¨μ§ lib.d.ts
μ μ μΈλ λΉ μΈν°νμ΄μ€μ
λλ€. κ°μ²΄ 리ν°λ΄μ λ¬Έλ§₯μ νμ
μΌλ‘ μΈμλλ κ²μ λμ΄, κ·Έ μΈν°νμ΄μ€λ λΉ μΈν°νμ΄μ€μ²λΌ λμν©λλ€.