• TypeScript๋ ๊ณตํต ํ์ ๋ณํ์ ์ฉ์ดํ๊ฒ ํ๊ธฐ ์ํด ์ ํธ๋ฆฌํฐ ํ์ ์ ์ ๊ณตํ๋ค.
• ์ ํธ๋ฆฌํฐ ํ์ ์ ์ ์ญ์ผ๋ก ์ฌ์ฉ ๊ฐ๋ฅํ๋ค.
์ข ๋ฅ
- Partial
- Readonly
- Record
- Pick
- Omit
- Exclude
- Extract
- NonNullable
- Parameter
- ConstructorParameters
- ReturnType
- Required
1. Partial<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',
});
- ํ๋กํผํฐ๋ฅผ ์ ํ์ ์ผ๋ก ๋ง๋๋ ํ์ ์ ๊ตฌ์ฑํ๋ค.
- ์ฃผ์ด์ง ํ์ ์ ๋ชจ๋ ํ์ ํ์ ์งํฉ์ ๋ํ๋ด๋ ํ์ ์ ๋ฐํํ๋ค.
2. Readonly<T>
interface Todo {
title: string;
}
const todo: Readonly<Todo> = {
title: 'Delete inactive users',
};
// ์ฌ์ฉ ์: frozen ๊ฐ์ฒด์ ํ๋กํผํฐ์ ์ฌํ ๋น ๋ฐฉ์ง
todo.title = 'Hello';
// Error: Cannot as sign to 'title' because it is a read-only property
ํ๋กํผํฐ๋ฅผ ์ฝ๊ธฐ ์ ์ฉ(readonly)์ผ๋ก ์ค์ ํ ํ์ ์ ๊ตฌ์ฑํ๋ค.
๊ฐ์ฒด ์์ฑ์ ํ๋กํผํฐ ๊ฐ์ ๋ฐ๊ฟ ์ ์๋ค.
ํ๋กํผํฐ๋ฅผ ์ํด ์ฌ์ฉํ ์ ์๋ const์ด๋ค.
3. Record<T,K>
interface PageInfo {
title: string;
}
type Page = 'home' | 'about' | 'contact';
const x: Record<Page, PageInfo> = {
about: { title: 'about' },
contact: { title: 'contact' },
home: { subTitile: 'home' },
// Error: '{ subTitile: string; }' is not assignable
main: { title: 'home' },
// Error: main is not assignable to type 'Page'.
};
ํ๋กํผํฐ์ ์งํฉ K๋ก ํ์ ์ ๊ตฌ์ฑํ๋ค.
ํ์ ์ ํ๋กํผํฐ๋ค์ ๋ค๋ฅธ ํ์ ์ ๋งคํ์ํค๋ ๋ฐ ์ฌ์ฉํ๋ค.
4. Pick<T,K>
interface Todo {
title: string;
description: string;
completed: boolean;
}
type TodoPreview = Pick<Todo, 'title' | 'completed'>;
const todo: TodoPreview = {
title: 'Clean room',
completed: false,
description: 'description’
// Error: 'description' is not assignable to type
};
ํ๋กํผํฐ K์ ์งํฉ์ ์ ํํด ํ์ ์ ๊ตฌ์ฑํ๋ค
5. Omit<T,K>
interface Todo {
title: string;
description: string;
completed: boolean;
}
type TodoPreview = Omit<Todo, 'description'>;
const todo: TodoPreview = {
title: 'Clean room',
completed: false,
description: 'description', // Error: 'description' is not assignable to type
};
๋ชจ๋ ํ๋กํผํฐ๋ฅผ ์ ํํ ๋ค์ K๋ฅผ ์ ๊ฑฐํ ํ์ ์ ๊ตฌ์ฑํ๋ค.
6. Exclude<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์ ํ ๋นํ ์ ์๋ ๋ชจ๋ ์์ฑ์ ์ ์ธ ํ ํ์ ์ ๊ตฌ์ฑํ๋ค.
7. Extract<T,U>
type T0 = Extract<'a' | 'b' | 'c', 'a' | 'f'>;
// "a"
type T1 = Extract<string | number | (() => void), Function>;
// () => void
T์์ U์ ํ ๋น ํ ์ ์๋ ๋ชจ๋ ์์ฑ์ ์ถ์ถ ํ์ฌ ํ์ ์ ๊ตฌ์ฑํ๋ค.
8. NonNullable<T>
type T0 = NonNullable<string | number | undefined>;
// string | number
type T1 = NonNullable<string[] | null | undefined>;
// string[]
null ๊ณผ undefined๋ฅผ ์ ์ธํ ํ์ ์ด๋ค.
9. Parameters<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>; // ์ค๋ฅ
ํจ์ ํ์ T์ ๋งค๊ฐ๋ณ์ ํ์ ๋ค์ ํํ ํ์ ์ ๊ตฌ์ฑํ๋ค.
10. ConstructorParameters<T>
type T10 = ConstructorParameters<ErrorConstructor>; // [(string | undefined)?]
type T1 = ConstructorParameters<FunctionConstructor>; // string[]
type T2 = ConstructorParameters<RegExpConstructor>; // [string, (string | undefined)?]
interface I1 {
new (args: string): Function;
}
type T12 = ConstructorParameters<I1>; // [string]
function f1(a: T12) {
a[0];
a[1]; // Error: Tuple type '[args: string]' of length '1' has no element at index '1'.
}
์์ฑ์ ํจ์ ํ์ ์ ๋ชจ๋ ๋งค๊ฐ๋ณ์ ํ์ ์ ์ถ์ถํ๋ค.
๋ชจ๋ ๋งค๊ฐ๋ณ์ ํ์ ์ ๊ฐ์ง๋ ํํ ํ์ (T๊ฐ ํจ์๊ฐ ์๋ ๊ฒฝ์ฐ never)์ ์์ฑํ๋ค.
11. ReturnType<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์ ๋ฐํ ํ์ ์ผ๋ก ๊ตฌ์ฑ๋ ํ์ ์ ์์ฑํ๋ค.
12. Required<T>
interface Props {
a?: number;
b?: string;
}
const obj: Props = { a: 5 };
const obj2: Required<Props> = { a: 5 };
// Error: Property 'b' is missing in type '{ a:number; }
T์ ๋ชจ๋ ํ๋กํผํฐ๊ฐ ํ์๋ก ์ค์ ๋ ํ์ ์ ๊ตฌ์ฑํ๋ค.
'๐ Front > ๐ซ Typescript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Typescrip] ํ์ ๊ฐ๋ (intersection, union, Type Guard) ๊ฐ๋ ์ฌ๋ ค! (0) | 2022.03.22 |
---|---|
[Typescript] ํจ์ ์ฌ์ฉํ๊ธฐ (parameter, optional, rest, overloading) (0) | 2022.03.22 |
[React+Typescript] React.FC๋ฅผ ๊ตณ์ด ์ฌ์ฉํ์ง ์์๋ ๋๋ ์ด์ (0) | 2021.09.06 |
[Typescript]: type, interface, generics ์ฌ์ฉํ๊ธฐ (0) | 2021.09.06 |
Typescript ์์ฑ์, public, private (0) | 2021.09.06 |