...

ํ์ ์คํฌ๋ฆฝํธ ๋งต๋ ํ์
ํ์ ์คํฌ๋ฆฝํธ์ ๊ณ ๊ธ ํ์ ์ธ ๋งต๋ ํ์ (mapped type)์ด๋ ๊ธฐ์กด์ ์ ์๋์ด ์๋ ํ์ ์ ์๋ก์ด ํ์ ์ผ๋ก ๋ณํํด ์ฃผ๋ ๋ฌธ๋ฒ์ ์๋ฏธ ํ๋ค.
์๋ฅผ ๋ค์ด ์ธํฐํ์ด์ค์ ์๋ ๋ชจ๋ ์์ฑ์ ๋ฃจํ๋ฌธ ๊ฐ์ด ์ํํด์ optional(?) ๋ก ๋ฐ๊พธ๊ฑฐ๋ readonly ๋ก ์ง์ ํ ์ ์์ผ๋ฉฐ, ์์ ์ง์ ๋ ํ์ ์ ๋ฐ๊ฟ์ ๋ณ๊ฒฝ๋ ํ์ ์ ๋ฐํํ ์ ๋ ์๋ค.

์์ง ๋งต๋ ํ์ ์ ๋ํด ๋ฌธ๋ฒ์ ๋ฐฐ์ฐ์ง๋ ์์์ง๋ง ๊ฐ๋จํ๊ฒ ์ดํด๋ณด์๋ฉด ๋ค์๊ณผ ๊ฐ๋ค.
interface Obj {
prop1: string;
prop2: string;
}
type ChangeType<T> = {
[K in keyof T]: number;
};
type Result = ChangeType<Obj>;
/*
{
prop1: number;
prop2: number;
}
*/
Obj ๋ผ๋ ์ธํฐํ์ด์ค์ ๊ฐ์ฒด ์์ฑ ํ์
string์ ChangeType<Obj> ์ ํตํด Obj ํ์
๋ค์ number๋ก ๋ชจ๋ ๋ฐ๊ฟ์ฃผ๊ณ Result ํ์
๋ณ์นญ์๊ฒ ๋ฐํํ์๋ค.
๊ทธ๋์ ๊ฒฐ๊ณผ์ ์ผ๋ก ํ์
Result๋ { prop1: number; prop2; number } ์ ๊ฐ์ ๊ฐ์ฒด ํ์
์ ๊ฐ์ง๊ฒ ๋์๋ค.
์ด๋ ๋ง์น ์๋ฐ์คํฌ๋ฆฝํธ๋ก ๋ฐ์ง๋ฉด, ๊ฐ์ฒด์ ์์ฑ์ value๋ฅผ ํจ์์์ for in ์ผ๋ก ๊ฐ์ฒด๋ฅผ ์ํํด ๊ฐ ์์ฑ์ ๊ฐ๋ค์ ๋ฌธ์์ด์์ ์ซ์๋ก ๋ฐ๊ฟ์ฃผ๋ ๊ฒ๊ณผ ๋น์ทํด ๋ณด์ธ๋ค.
const Obj = {
prop1: "ํ๊ธธ๋",
prop2: "ํ๊ธธ๋"
}
function ChangeValue(T) {
for(let K in T) { T[K] = 1000; }
return T;
}
const Result = ChangeValue(Obj);
/*
{
prop1: 1000,
prop2: 1000
}
*/
ํ์ ์คํฌ๋ฆฝํธ๋ ํ์ ์ ๋ฐ๋ก ๋ค๋ฃจ๋ ์ธ์ด์ด๋, ์์ ๋ก์ง์์ value๊ฐ ์๋๋ผ type์ผ๋ก ๋ฐ๊ฟ ์๊ฐํด๋ณด๋ฉด ํ๋ฐ์ด๋ผ๊ณ ๋ณด๋ฉด ๋๋ค.
๋งต๋ ํ์ ๋ฌธ๋ฒ

์ด์ฒ๋ผ ๋งต๋ ํ์ ์ ๊ฐ์ฒด์ ์์ฑ๋ค์ ์ํํด์ ์์ฑ์ ํ์ ์ ๋ค๋ฅธ ํ์ ์ผ๋ก ๋ฐ๊ฟ์ฃผ๋ ์ญํ ์ ํ๋ค.
๊ฐ์ฒด ํ์ ์ ์์ฑ๋ค์ ์ํํ๊ธฐ ๋๋ฌธ์ ์ด๋ฅผ ์์ฉํด์, ๋ชจ๋ ๊ฐ์ฒด์ ์์ฑ๋ค์ ์ํํด์ optional(?) ๋ก ๋ฐ๊พธ๊ฑฐ๋ readonly ๋ก ์ง์ ํ ์๋ ์๋ค.
๊ธฐ์กด์๋ ์ด๋ ๊ฒ ์ธํฐํ์ด์ค๋ก ์ผ์ผํ ๋ฐ๋ก๋ฐ๋ก ์ง์ ํ๋๊ฑธ,
interface PersonPartial {
name?: string;
age?: number;
}
interface PersonReadonly {
readonly name: string;
readonly age: number;
}
๋งต๋ ํ์ ๋ฌธ๋ฒ์ ์ด์ฉํด์ ๋ง์น ํจ์๋ฅผ ์ด์ฉํ๋ ๊ฒ์ฒ๋ผ ์์ฑ๋ค์ ์ํํด์ ๋ณ๊ฒฝํด์ฃผ๊ณ ๊ทธ ๊ฒฐ๊ณผ๊ฐ์ type alias์๊ฒ ๋ณํํด ์ค๋ค. ๋ณด๋ค ์ํผ ๋งต๋ ํ์ ์ ์ ๋ค๋ฆญ๊ณผ ๊ฒฐํฉํ๋ฉด ๋งค์ฐ๋งค์ฐ ๊ฐ๋ ฅํด์ง๋ค.
interface Person {
name: string;
age: number;
}
type ReadOnly<T> = {
readonly [P in keyof T]: T[P];
};
type ParTial<T> = {
[P in keyof T]?: T[P];
};
type PersonPartial = Partial<Person>;
type ReadonlyPerson = Readonly<Person>;

์ด์ ๋งต๋ ํ์ ์ด๋ผ๋ ๊ธฐ๋ฅ์ ๋ํด ์ต์ํด์ก์ผ๋, ๋งต๋ ํ์ ๊ตฌ์ฑ ๋ฌธ๋ฒ์ ๋ํด ์ฐจ๊ทผ์ฐจ๊ทผ ์์๋ณด์.
interface Obj {
name: string;
email: string;
}
type ObjNumber = {
[P in keyof Obj]: number // ๋งต๋ ํ์
};
/*
type ObjNumber = {
name: number;
email: number;
}
*/
์ ์ฝ๋์์ ๋ณด๋ค์ํผ, ๋งต๋ ํ์ ์ ๋ง๋ค์ด์ง๋ ๋ฐํ ๊ฐ์ด ๊ฐ์ฒด ํ์ ํํ์ด๊ธฐ ๋๋ฌธ์ ์ค๊ดํธ๋ก ๋๋ฌ์์ฌ์ ธ ์์ผ๋ฉฐ, ์ค๊ดํธ ์์ ๋๊ดํธ๋ ํค ๋ถ๋ถ์ ๋ํ๋ธ๋ค.
๋ง์น ์ธํฐํ์ด์ค์ indexable ํ์ ์ ์๊ฐํ๋ฉด ๋๋ค.

๊ทธ๋ฐ๋ฐ ์ธํฐํ์ด์ค์ indexable ํ์ ๊ณผ ๋ค๋ฅธ์ ์ด ์๋๋ฐ ๋ฐ๋ก ๋๊ดํธ ์์ in ํค์๋๋ฅผ ์ฌ์ฉํ๋ค๋ ์ ์ด๋ค.
in ์ฐ์ฐ์๋ ์๋ฐ์คํฌ๋ฆฝํธ์ for in ์ผ๋ก ์๊ฐํ๋ฉด ํธํ๋ค.
for in ์ ๊ฐ์ฒด์ key๋ฅผ ์ํํ๋ ๋ฃจํ๋ฌธ์ธ๋ฐ ์ด ์ญํ ์ ๋๊ฐ์ด ๋ฐ๋ผํ๋ค๊ณ ๋ณด๋ฉด ๋๋ค.
๊ทธ๋ฆฌ๊ณ ๋๊ดํธ ์์ key์ ๋ช
์นญ์ indexable ํ์
์ด๋ ์ ๋ค๋ฆญ ๊ฐ์ด ๋ง์๋๋ก ์ง์ด๋๋๋ค. for in ์ผ๋ก ๋ฐ์ง๋ฉด for (let i in obj) ์์์ ๋ณ์ i ์ ๊ฐ์ ๊ฐ๋
์ด๋ค. (in์ผ๋ก ์ํํ๋ ๊ฐ๋ค์ ์ ์ฅํ๋ ์์ ๋ณ์)
๋ง์ง๋ง์ผ๋ก keyof ๋ ์ธํฐํ์ด์ค Obj์ ๊ฐ์ฒด ํ์
์์ฑ๋ค์ ๋ฝ์ ์ ๋์จ์ผ๋ก name | email ๋ง๋ค์ด์ฃผ๋ ์ญํ ์ ํ๋ค. (์ฐธ๊ณ ๊ธ)
๋งต๋ ํ์ ํ์ฉ ์์
๋งต๋ ํ์ ์ ๊ทธ์ ๋ ์ฌ๋ฆฌ๋ ๊ฒ๋ณด๋จ, ์ค์ ์์ ์ฝ๋๋ฅผ ํตํด ์ตํ๋ ๊ฒ์ด ์ข๋ค.
๋ค์ ์์ ์ฝ๋๋ค์ ๋ณด๋ฉฐ ๋จธ๋ฆฟ์์ ์ตํ๋ณด์.
๋งต๋ ํ์ ์ผ๋ก ๊ฐ์ฒด ํ์ ์์ฑ
์ ๋์จ์ผ๋ก ์์ฑ๋ช ๋ค์ ์ง์ ํด์ฃผ๊ณ in ํค์๋๋ก ์ํํด ์ฃผ๋ฉด์ ์ง์ ํ ์๋ช ๋ค์ ํ์ ์ boolean ์ด๋ number ๋ก ์ง์ ํ๊ณ , ์์ฑ๋ ํ์ ์ ํ์ ๋ณ์นญ์ ๋ฐํํ๋ ์์ ์ด๋ค.
type T1 = {
[K in "prop1" | "prop2"]: boolean
};
/*
type T1 = {
prop1: boolean;
prop2: boolean;
}
*/
type T2 = {
[K in "prop1" | "prop2"]: number
};
/*
type T2 = {
prop1: number;
prop2: number;
}
*/
type T3 = {
[K in "prop1" | "prop2"]: string
};
/*
type T3 = {
prop1: string;
prop2: string;
}
*/
๋งต๋ ํ์ ์ ์ ๋ค๋ฆญ๊ณผ ๊ฒฐํฉํด์ ์ฌ์ฉํ๋ฉด ๋ง์น ์๋ฐ์คํฌ๋ฆฝํธ ํจ์ ์ฒ๋ผ ํ์ ์ ๋ง๋ค๊ณ ๋ฐํํ๋ ๋ก์ง์ ๊ตฌ์ฑ ํ ์ ์๋ค.
type Prop = 'prop1' | 'prop2';
type Make<T> = {
[K in Prop]: T
};
type T1 = Make<boolean>;
/*
type T1 = {
prop1: boolean;
prop2: boolean;
}
*/
type T2 = Make<number>;
/*
type T2 = {
prop1: number;
prop2: number;
}
*/
type T3 = Make<string>;
/*
type T3 = {
prop1: string;
prop2: string;
}
*/
์ด๋ ๊ฒ ๋ง๋ค์ด์ง ํ์ ์ผ๋ก ๊ฐ์ฒด ๋ณ์๋ฅผ ์ ์ธํ์ฌ ์ด์ฉํ๋ฉด ๋๋ค.
const obj1: T1 = {
prop1: true,
prop2: false,
};
const obj2: T2 = {
prop1: 123,
prop2: 66666,
};
const obj3: T3 = {
prop1: 'hello',
prop2: 'world',
};
๋งต๋ ํ์ ์ผ๋ก ๊ฐ์ฒด ์์ฑ ์ ๊ฑฐ
์๋์ ๊ฐ์ด ์ฌ์ฉ์ ํ๋กํ์ ์กฐํํ์ฌ ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ API ํจ์๊ฐ ์๋ค๊ณ ๊ฐ์ ํ๋ค.
interface UserProfile {
username: string;
email: string;
profilePhotoUrl: string;
}
// ๋ง์ฝ api๋ฅผ ์์ฒญํด ์ ์ ํ๋กํ์ผ์ ์๋ตํ๋ http ํจ์๋ผ๊ณ ๊ฐ์ ํ๋ค๋ฉด
function fetchUserProfile(): UserProfile {
return {
username: 'ํ๊ธธ๋',
email: 'hongildong@naver.com',
profilePhotoUrl: 'image',
};
}
const user: UserProfileUpdate = fetchUserProfile();
๋ง์ผ ์ฌ์ฉ์๊ฐ ์น์์ ํ๋กํ ์ ๋ณด๋ฅผ ์์ ํ๋ค๋ฉด, ์ด ํ๋กํ ๊ฐ์ฒด์ ์ ๋ณด๋ฅผ ์์ ํ๋ API๋ UserProfileUpdate ๋ผ๊ณ ๊ฐ์ ํ์.
์ฌ๊ธฐ์ optional ํค์๋๋ง ๋ถ์ธ ๋๊ฐ์ ํํ์ ์ธํฐํ์ด์ค๋ฅผ ์์ฑํ๋ ์ด์ ๋, ๊ธฐ์กด์ UserProfile ์ธํฐํ์ด์ค ํ์ ์ ๊ฐํ๊ฒ ํ์ ์ด ์ ํด์ ธ์๊ธฐ ๋๋ฌธ์, ๊ฐ์ฒด์ ์์ฑ์ ์ญ์ ํ๊ธฐ ์ํด์๋ ์ด๋ฐ์์ ์กฐ์น๋ฅผ ์ทจํด ์ฃผ์ด์ผ ํ๋ค.
interface UserProfileUpdate {
username?: string;
email?: string;
profilePhotoUrl?: string;
}
// ์ฌ์ฉ์๊ฐ ์์ ์ ํ๋กํ ์ด๋ฏธ์ง๋ฅผ ์ญ์ ์ฒ๋ฆฌ๋ฅผ ํ์ฌ, ์ ์ ๊ฐ์ฒด์์ ํ๋กํ์ผ ์ด๋ฏธ์ง url ์์ฑ์ ์ญ์ ํด์ ์
๋ฐ์ดํธํ๋ api
function updateUserProfile(params: UserProfileUpdate) {
delete params.profilePhotoUrl;
}
updateUserProfile(user);
ํ์ง๋ง ๊ฐ์ ๋ชจ์์ ์ธํฐํ์ด์ค๋ฅผ ๋ฐ๋ณตํด์ ์ ์ธํ๋ ๊ฒ์ ๊ฐ๋ ์ฑ์ ๋งค์ฐ ์์ข์ผ๋ ํผํด์ผ ํ๋ค.
interface UserProfile {
username: string;
email: string;
profilePhotoUrl: string;
}
interface UserProfileUpdate {
username?: string;
email?: string;
profilePhotoUrl?: string;
}
๊ทธ๋์ ์์ ์ธํฐํ์ด์ค์์ ๋ฐ๋ณต๋๋ ๊ตฌ์กฐ๋ฅผ ์๋์ ๊ฐ์ ๋ฐฉ์์ผ๋ก ์ฌํ์ฉ ํ ์ ์๋ค.
interface UserProfile {
username: string;
email: string;
profilePhotoUrl: string;
}
type UserProfileUpdate = {
[p in 'username' | 'email' | 'profilePhotoUrl']?: UserProfile[p];
};
/*
type UserProfileUpdate = {
username?: string | undefined;
email?: string | undefined;
profilePhotoUrl?: string | undefined;
}
*/
์ฌ๊ธฐ์ ์ ์ฝ๋์ keyof ์ธํฐํ์ด์ค ๋ฅผ ์ ์ฉํ๋ฉด, ์๋์ผ๋ก ๊ฐ์ฒด ์์ฑ๋ค์ ๋ชจ์ ์ ๋์จ ํ์
์ผ๋ก ๋ณํํด์ค์, ์๋์ ๊ฐ์ด ์ค์ผ ์ ์๋ค.
interface UserProfile {
username: string;
email: string;
profilePhotoUrl: string;
}
type UserProfileUpdate = {
[p in keyof UserProfile]?: UserProfile[p];
};
์ธํฐํ์ด์ค ํ์ ๋ฐ๊พธ๊ธฐ
interface Person {
name: string;
age: number;
}
type MakeBoolean<T> = {
[P in keyof T]?: boolean
};
const pMap: MakeBoolean<Person> = {}; // Person ์ธํฐํ์ด์ค์ ํ์
๋ค์ ๋ชจ๋ boolean์ผ๋ก ๋ณ๊ฒฝํ๊ณ optional๋ก ์ง์
/*
{
name?: boolean | undefiened;
age?: boolean | undefiened;
}
*/
pMap.name = true;
pMap.age = false;
pMap.age = undefined; // ์ ํ ์์ฑ์ด๊ธฐ์ undefiened ํ ๋น ๊ฐ๋ฅ
readonly / optional ๋ถ์ด๊ธฐ
์์์ ํ๋ฒ ์๊ฐํ ๋งต๋ ํ์ ์ ๋ํ์ ์ธ ๊ธฐ๋ฒ์ด๋ค.
๊ทธ๋ฐ๋ฐ ์ฌ๊ธฐ์ ์ฃผ์ ๊น๊ฒ ๋ด์ผ ํ ์ ์ T[P] ๋ถ๋ถ์ธ๋ฐ, ์ด ํค์๋์ ์๋ฏธ๋ ์ ๋ค๋ฆญ์ผ๋ก ๋ฐ์ ์์ฑ์ ํ์
์ ์ ์งํ๋ค ๋ผ๋ ์๋ฏธ์ด๋ค.
์๋ฅผ ๋ค์ด Partial<Person> ๋งต๋ ํ์
์ ํธ์ถํ๋ฉด, ์ ๋ค๋ฆญ T์ Person์ด ๋ค์ด์ฌ๊บผ๊ณ , ๋งต๋ ํ์
P์ Person์ keyof์ธ (name | age) ์ ๋์จ ํ์
์ด ์ํ๋์ด ํ๋์ฉ P์ ๋ค์ด์ค๊ฒ ๋๋ค.
๊ฒฐ๊ตญ T[P] ๋ Person[name] ๊ณผ Person[age] ๊ฐ ๋ ๊ฒ์ด๊ณ , ์ด๋ ๊ณง ์์ฑ ์๊ธฐ ์์ ์ ํ์
๋ฐธ๋ฅ๋ฅผ ๊ฐ๋ฆฌํค๋ ๊ฒ์ด๋, ์์ ์ ์์ฑ์ ํ์
์ ๋ณต์ฌํด ๊ทธ๋๋ก ๋ฐํํ๋ค๋ ์๋ฏธ์ด๋ค.
interface Person {
name: string;
age: number;
}
type Readonly<T> = {
readonly [P in keyof T]: T[P];
};
type Partial<T> = {
[P in keyof T]?: T[P];
};
type PersonPartial = Partial<Person>;
/*
type PersonPartial = {
name?: string | undefined;
age?: number | undefined;
}
*/
type ReadonlyPerson = Readonly<Person>;
/*
type ReadonlyPerson = {
readonly name: string;
readonly age: number;
}
*/
readonly / optional ๋ผ๋ฒ๋ฆฌ๊ธฐ
์์ฑ๋ค์ ์ํํด์ readonly ์ ? ๋ฅผ ๋ถ์ฌ๋ดค์ผ๋ฉด ์ด๋ฒ์ ์ ๊ฑฐํด๋ณด๋ ๋งต๋ ํ์ ์ ๋ง๋ค์ด ๋ณด์.
์ฌ๊ธฐ์ ์๋ก์ด ์ฐ์ฐ์๊ฐ ๋์ค๋๋ฐ ๋ฐ๋ก ๋ง์ด๋์ค - ์ฐ์ฐ์์ด๋ค. ์ด๋ ต๊ฒ ์ดํดํ ํ์์์ด ์ ๊ฑฐ์ ์๋ฏธ๋ฅผ ๋ดํฌํ๋ค.
interface Person1 {
name?: string;
age?: number;
}
interface Person2 {
readonly name: string;
readonly age: number;
}
type Exclude_ReadOnly<T> = {
// -readonly ๋ผ๋ ๋ป์ readonly๋ฅผ ๋ผ๋ผ ๋ผ๋ ์๋ฏธ์ด๋ค.
// ๋ฐ๋๋ก +readonly ๋ readonly๋ฅผ ์ถ๊ฐํ๋ผ๋ ์๋ฏธ์ธ๋ฐ ์ ์๋ฅผ +1๋ก ์์ฐ๋ฏ์ด ๊ทธ๋ฅ ์๋ต ๊ฐ๋ฅํจ
-readonly [P in keyof T]: T[P];
};
type Exclude_ParTial<T> = {
// -? ๋ผ๋ ๋ป์ ?๋ฅผ ๋ผ๋ผ ๋ผ๋ ์๋ฏธ์ด๋ค.
// ๋ฐ๋๋ก +? ๋ ?๋ฅผ ์ถ๊ฐํ๋ผ๋ ์๋ฏธ์ธ๋ฐ ์ ์๋ฅผ +1๋ก ์์ฐ๋ฏ์ด ๊ทธ๋ฅ ์๋ต ๊ฐ๋ฅํจ
[P in keyof T]-?: T[P];
};
type PersonPartial = Exclude_ParTial<Person1>;
/*
type PersonPartial = {
name: string;
age: number;
}
*/
type ReadonlyPerson = Exclude_ReadOnly<Person2>;
/*
type ReadonlyPerson = {
name: string;
age: number;
}
*/
์ค์ฒฉ ๊ฐ์ฒด ์์ฑ ๋ง๋ค๊ธฐ
๋งต๋ ํ์ ์ ์ด์ฉํด ์ค์ฒฉ ๊ฐ์ฒด ์์ฑ ํ์ ์ ์ด๋ฃจ๋ ์์ ์ด๋ค.
/* eslint-disable */
export {};
interface Person {
name: string;
age: number;
language: string;
}
type Recorded<K extends string, T> = { [P in K]: T };
type T1 = Recorded<'p1' | 'p2', Person>;
const t: T1 = {
p1: {
name: 'ํ๊ธธ๋',
age: 88,
language: 'kor',
},
p2: {
name: '๋ง์ปจ',
age: 34,
language: 'eng',
},
};
๋งต๋ ํ์ ์ ์ ๋ฉค๋ฒ ์ถ๊ฐ
๋ฉค๋ฒ๋ฅผ ์ถ๊ฐํ๊ธธ ์ํ๋ค๋ฉด, ๊ต์ฐจ ํ์ ์ ์ด์ฉํด ๊ตฌ์ฑํ ์ ์๋ค
interface Person {
name: string;
age: number;
}
type PartialWithNewMember<T> = {
[P in keyof T]?: T[P];
} & { newMember: boolean };
type PersonNew = PartialWithNewMember<Person>;
/*
type PersonNew = {
name?: string | undefined;
age?: number | undefined;
newMember: boolean;
}
*/
const person2: PersonNew = {
name: 'ํ๊ธธ๋',
newMember: true,
};
Enum ํ์ ๊ฐ๋ ํ๊ธฐ
๋งต๋ ํ์ ์ ์ด์ฉํ๋ฉด Enum ํ์ ์ ํ์ฉ๋๋ฅผ ๋์ผ ์ ์๋ค.
enum Fruit {
Apple,
Banana,
Orange,
}
const FRUIT_PRICE: { [key in Fruit]: number } = {
[Fruit.Apple]: 1000,
[Fruit.Banana]: 1500,
[Fruit.Orange]: 2000,
};
์๋ฅผ ๋ค์ด์ ์์ ์ฝ๋์ ๊ฐ์ด Fruit ๋ผ๋ enum ํ์ ์ด ์๋๋ฐ, FRUIT_PRICE ๋ผ๋ ์์์์ ๋ชจ๋ ๊ณผ์ผ ๊ฐ๊ฒฉ์ ๊ด๋ฆฌํ๋ค๊ณ ๊ฐ์ ์ ํด๋ณด๋ฉด, ๋ง์ฝ enum ํ์ ์ด ๋ณํ๊ฐ ์์๋ ๋ค์๊ณผ ๊ฐ์ด ๋นจ๊ฐ์ค๋ก ๊ฒฝ๊ณ ๋ฅผ ํด์ค๋ค.


์ด์ฒ๋ผ ์ฝ๋ ์์ ์ด ํ์ํ๋ฉด ๋งต๋ ํ์ ์ ์ํด์ ๋นจ๊ฐ์ค์ด ๋์ ์ค์๋ฅผ ์ค์ผ ์ ์๋ค.
์ ํธ๋ฆฌํฐ ํ์ ์๊ฐ
๋งต๋ ํ์ ์ ์๋ฆฌ๋ฅผ ์ดํดํ๋๋ฐ ์์ด ์ด ๊ธ์ ๋๊น์ง ๋ณด์ ์ฌ๋ฌ๋ถ๋ค๊ป ๋ ธ๊ณ ๋ฅผ ์นํํ๋ค.

์ด์ฒ๋ผ ๋งต๋ ํ์ ๋ฌธ๋ฒ์ ์ด์ฉํด ์ถฉ๋ถํ ํ์ ์ ๋ณํํ ์ ์์ง๋ง, ์ ํธ๋ฆฌํฐ ํ์ ์ ์ฐ๋ฉด ์ด๋ฏธ ์ ์ํด ๋์ ํ์ ์ ๋ณํํ๊ฑฐ๋ ์ฝ๋๋ฅผ ์ค์ฌ ๊ฐ๊ฒฐํ๊ฒ ์ ์ํ ์ ์๋ค.
์ฌ์ค ์์์ ๋ค๋ฃจ์๋, Readonly ๋ Partial, Pick ๋ฑ์ ์๋ ์ด๋ฏธ ํ์ ์คํฌ๋ฆฝํธ ๊ฐ๋ฐ์ง๋ค์ด ๋ง๋ค์ด ๋์ ๋งต๋ ํ์ ๋ค์ด๋ค. ์ด๋ฌํ ๋ฏธ๋ฆฌ ๋ง๋ค์ด์ง ๋งต๋ ํ์ ์ ์ ํธ๋ฆฌํฐ ํ์ ์ด๋ผ๊ณ ๋ถ๋ฅธ๋ค๊ณ ์ดํดํ๋ฉด ๋๋ค.
์ ํธ๋ฆฌํฐ ํ์ ์ ๋ํด์๋ ๋ค์ ํฌ์คํ ์ ์ฐธ๊ณ ํ๊ธธ ๋ฐ๋๋ค.
[TS] ๐ ํ์ ์คํฌ๋ฆฝํธ - ์ ํธ๋ฆฌํฐ ํ์ ๐ฏ ์ด์ ๋ฆฌ (+ ์์ฉ)
ํ์ ์คํฌ๋ฆฝํธ - Utility Types ์ง๊ธ๊น์ง ํ์ ์คํฌ๋ฆฝํธ๋ฅผ ๋ค๋ฃจ๋ฉด์, ์๋ฐ์คํฌ๋ฆฝํธ๋ฅผ ์ด๋์ ๋ ์๋๊น ํ์ ์ข ๋ฅ๋ง ๋ฐฐ์ฐ๋ฉด ๋๋ฑ ๋ง์คํฐ ํ ์ค ์์๋๋, ํ์ ์์ฒด๋ฅผ ์ฝ๋ฉํ๋ฉฐ ์๋ฌ์ค์ ์ฌ๋ผ์ง๊ฒ ํ
inpa.tistory.com
interface Product {
name: string;
phone: number;
address: string;
company: string;
}
type UpdateProduct = Partial<Product> // ๋ชจ๋ ์์ฑ์ ์ต์
๋๋ก ๋ง๋ค์ด์ฃผ๋ ์ ํธ๋ฆฌํฐ ํ์
/*
type UpdateProduct = {
name?: string | undefined;
phone?: number | undefined;
address?: string | undefined;
company?: string | undefined;
}
*/
function updateProductItem(productItem: UpdateProduct) {
}
# ์ฐธ๊ณ ์๋ฃ
https://typescript-kr.github.io/pages/advanced-types.html
์ด์ฌ์น ํ์ ์คํฌ๋ฆฝํธ ์์ํ๊ธฐ
์ ๋ก์ด ํ์ ์คํฌ๋ฆฝํธ ์ฌ์ธ์
์ฝ๊ฒ ์์ํ๋ ํ์ ์คํฌ๋ฆฝํธ (๊ธธ๋ฒ, 2023, ์บกํดํ๊ต ์ง์)
์ด ๊ธ์ด ์ข์ผ์ จ๋ค๋ฉด ๊ตฌ๋ & ์ข์์
์ฌ๋ฌ๋ถ์ ๊ตฌ๋
๊ณผ ์ข์์๋
์ ์์๊ฒ ํฐ ํ์ด ๋ฉ๋๋ค.