TypeScript
π TypeScript μ νΈλ¦¬ν° νμ κ³Ό νμ©
TypeScriptλ μ νΈλ¦¬ν° νμ
(Utility Types)μ μ 곡νμ¬ κΈ°μ‘΄ νμ
μ λ³ννκ±°λ μλ‘μ΄ νμ
μ μ½κ² μμ±ν μ μλλ‘ μ§μν©λλ€. νΉν, Omit
, Pick
, Partial
κ°μ νμ
μ 볡μ‘ν κ°μ²΄ μ§ν₯ νλ‘κ·Έλλ°κ³Ό νλ‘ νΈμλ μν κ΄λ¦¬ λΌμ΄λΈλ¬λ¦¬μμ λ§€μ° μ μ©νκ² μ°μ
λλ€.
π Omit
νμ
κ°μ
Omit
μ κΈ°μ‘΄ κ°μ²΄ νμ
μμ νΉμ μμ±μ μ μΈν νμ
μ μμ±ν©λλ€. λΆνμν μμ±μ μ κ±°νκ±°λ μΌλΆ μμ±μ μ¨κ²¨μΌ ν λ μ μ©ν©λλ€.
type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
μ¬μ©λ²
T
: λ³νν μλ³Έ νμ .K
: μ μΈν μμ±μ ν€(Key)λ€.
Omit
μ λ΄λΆμ μΌλ‘ Pick
κ³Ό Exclude
λ₯Ό μ¬μ©νμ¬, μ§μ λ μμ±μ μ μΈν μλ‘μ΄ νμ
μ μμ±ν©λλ€.
π Omit
μ¬μ© μμ
κΈ°λ³Έ μμ
interface User {
id: number;
name: string;
email: string;
password: string;
}
type PublicUser = Omit<User, "password">;
const user: PublicUser = {
id: 1,
name: "Alice",
email: "alice@example.com",
};
μ¬κΈ°μ PublicUser
λ User
νμ
μμ password
μμ±λ§ μ μΈν νμ
μΌλ‘ λ§λ€μ΄μ§λλ€. μ΄ λ°©μμ λ―Όκ°ν μ 보λ νμνμ§ μμ μμ±μ μ μΈνλ λ° λ§€μ° μ μ©ν©λλ€.
π TypeScript μ νΈλ¦¬ν° νμ νμ₯
Omit
μΈμλ λ€μν μ νΈλ¦¬ν° νμ
λ€μ΄ μμ΅λλ€. μ΄λ€μ μ‘°ν©νκ±°λ λ¨λ
μΌλ‘ μ¬μ©νλ©΄ λμ± κ°λ ₯ν νμ
μμ€ν
μ ꡬμΆν μ μμ΅λλ€.
1. Pick
νμ
Pick
μ κΈ°μ‘΄ κ°μ²΄ νμ
μμ νΉμ μμ±λ§ μ ννμ¬ μλ‘μ΄ νμ
μ μμ±ν©λλ€.
type Pick<T, K extends keyof T> = {
[P in K]: T[P];
};
μ¬μ© μμ
type UserBasicInfo = Pick<User, "id" | "name">;
const userBasic: UserBasicInfo = {
id: 1,
name: "Alice",
};
Pick
μ UIμ νΉμ λ°μ΄ν°λ§ νμν κ²½μ°λ μμ²/μλ΅μ λ°μ΄ν° ννλ₯Ό μ€μ΄κ³ μ ν λ μ μ©ν©λλ€.
2. Partial
νμ
Partial
μ λͺ¨λ μμ±μ μ νμ (optional)μΌλ‘ λ§λλ νμ
μ
λλ€. μ£Όλ‘ μ
λ°μ΄νΈμ© κ°μ²΄λ₯Ό μ μν λ μ μ©ν©λλ€.
type Partial<T> = {
[P in keyof T]?: T[P];
};
μ¬μ© μμ
type UpdateUser = Partial<User>;
const updateUser: UpdateUser = {
email: "new_email@example.com",
};
Partial
μ νΉμ μμ±λ§ μ
λ°μ΄νΈκ° νμν κ²½μ°μ νμ©λ©λλ€.
3. Required
νμ
Required
λ λͺ¨λ μμ±μ νμ(non-optional)λ‘ λ§λλ νμ
μ
λλ€. μ£Όλ‘ μ νμ μμ±μ κ°μ ν λ μ¬μ©λ©λλ€.
type Required<T> = {
[P in keyof T]-?: T[P];
};
μ¬μ© μμ
type FullUser = Required<Partial<User>>;
const fullUser: FullUser = {
id: 1,
name: "Alice",
email: "alice@example.com",
password: "secure",
};
4. Readonly
νμ
Readonly
λ λͺ¨λ μμ±μ μ½κΈ° μ μ©μΌλ‘ λ§λ€μ΄, κ°μ²΄λ₯Ό λΆλ³(Immutable) μνλ‘ μ μ§ν μ μμ΅λλ€.
type Readonly<T> = {
readonly [P in keyof T]: T[P];
};
μ¬μ© μμ
type ReadonlyUser = Readonly<User>;
const readonlyUser: ReadonlyUser = {
id: 1,
name: "Alice",
email: "alice@example.com",
password: "secret",
};
// readonlyUser.name = "Bob"; // Error: μ½κΈ° μ μ© μμ±μ
λλ€.
5. Record
νμ
Record
λ ν€(Key)μ κ°(Value)μ νμ
μ μ§μ νμ¬ κ°μ²΄ νμ
μ μμ±ν©λλ€.
type Record<K extends keyof any, T> = {
[P in K]: T;
};
μ¬μ© μμ
type PostStatus = "draft" | "published" | "archived";
type PostDirectory = Record<PostStatus, string>;
const postDirectory: PostDirectory = {
draft: "/drafts",
published: "/published",
archived: "/archives",
};
Record
λ λμ μΌλ‘ ν€λ₯Ό μ§μ ν΄μΌ νλ κ²½μ°μ μ ν©ν©λλ€.
6. Exclude
νμ
Exclude
λ λ νμ
μμ μ μΈν νμ
μ λͺ
μνμ¬ μλ‘μ΄ νμ
μ μμ±ν©λλ€.
type Exclude<T, U> = T extends U ? never : T;
μ¬μ© μμ
type AllActions = "create" | "update" | "delete" | "view";
type AdminActions = Exclude<AllActions, "view">;
const action: AdminActions = "create"; // OK
7. Extract
νμ
Extract
λ λ νμ
κ° κ³΅ν΅ νμ
μ μΆμΆν©λλ€.
type Extract<T, U> = T extends U ? T : never;
μ¬μ© μμ
type CommonActions = Extract<AllActions, "create" | "view">;
const action: CommonActions = "create"; // OK
8. NonNullable
νμ
NonNullable
λ null
κ³Ό undefined
λ₯Ό μ κ±°ν νμ
μ μμ±ν©λλ€.
type NonNullable<T> = T extends null | undefined ? never : T;
μ¬μ© μμ
type Name = string | null | undefined;
type DefinedName = NonNullable<Name>;
const name: DefinedName = "Alice"; // OK
π μ νΈλ¦¬ν° νμ μ‘°ν©
TypeScript μ νΈλ¦¬ν° νμ μ μ‘°ν©νμ¬ λμ± κ°λ ₯ν νμ μ λ§λ€ μ μμ΅λλ€.
μμ : API μλ΅ μ²λ¦¬
type ApiResponse<T> = Partial<Readonly<T>>;
type UserResponse = ApiResponse<Omit<User, "password">>;
const response: UserResponse = {
id: 1,
name: "Alice",
};
μ΄ μ½λλ API μλ΅μμ λΉλ°λ²νΈλ₯Ό μ μΈνκ³ μ½κΈ° μ μ© λ° μ νμ μμ±μ μΆκ°ν©λλ€.
π κ²°λ‘
TypeScriptμ μ νΈλ¦¬ν° νμ
μ 볡μ‘ν νμ
μ μλ₯Ό κ°κ²°νκ² λ§λ€κ³ , λ°λ³΅λλ μ½λ μμ±μ λΆλ΄μ μ€μ¬μ€λλ€. Omit
, Pick
, Partial
κ°μ νμ
μ μ μ ν νμ©νλ©΄ μ μ°νκ³ μ μ§λ³΄μ κ°λ₯ν μ½λ μμ±μ΄ κ°λ₯νλ©°, λμ± κ°λ ₯ν νμ
μμ€ν
μ ꡬμΆν μ μμ΅λλ€.
μ°Έκ³