νμ
μ€ν¬λ¦½νΈμλ κ΅μ₯ν λ§μ νμ
λ€μ΄ μ‘΄μ¬νκ³ μ€μ λ‘λ μμ£Ό μ¬μ©λμ§ μλ νμ
λ€λ λ§μ΄ μ‘΄μ¬ν©λλ€.
μλλ λ§μ κ°λ°μλ€μ΄ μ€λ¬΄μμ μ¬μ©νλ©΄μ κ°νΉ νΉμ μμ£Ό μ¬μ©νλ νμ
λ€ μ€ μ νΈλ¦¬ν° νμ
λ€μ
λλ€.
Utility Types | |
type | desc. |
enum βοΈ | μ¬λ¬ κ°λ€μ 미리 μ΄λ¦μ μ μνμ¬ μ΄κ±°ν΄ λκ³ μ¬μ©νλ νμ |
as const βοΈ | κ°μ μ¬ν λΉμ λ§μλ²λ¦¬κΈ° λλ¬Έμ μλμΉ μμ λ³κ²½μΌλ‘ μΈν μ€λ₯ β¬οΈ |
record | νΉμ νμ μ λͺ¨λ μμ±μ κ°μ μΌμ ν νμ μΌλ‘ 맀ν |
partial | λͺ¨λ νλ‘νΌν°λ₯Ό ?(optional)λ‘ λ§λ€μ΄ λ²λ¦¬λ νμ |
omit | νΉμ νμ μμ μ§μ λ μμ±μ μ μΈν λλ¨Έμ§ μμ±μ κ°μ§λ μλ‘μ΄ νμ μ μμ±νλ νμ |
pick | νΉμ νμ μμ μ§μ λ μμ±λ§μ μ ννμ¬ μλ‘μ΄ νμ μ μμ± |
extract | 첫λ²μ§Έ μ λ€λ¦ νμ Tμ λνμ¬ μ λ€λ¦ νμ U μ€ ν λΉ κ°λ₯ν νμ μ ν λΉ |
return type | νΉμ ν¨μμ μΆλ ₯μ λ€λ₯Έ ν¨μμμ κ°μ ΈμμΌ νλ μν©μμ λ§€μ° μ μ© |
optional | νΉμ νλ‘νΌν°λ₯Ό ?(optional)λ‘ λ§λ€μ΄ λ²λ¦¬λ νμ |
satisfies | μ£Όλ‘ μμ νκ² νμ νλͺ (Type Assertion)μ νκΈ° μν΄ μ¬μ© |
generic | μ¬μ¬μ©μ±μ΄ λμ μ»΄ν¬λνΈλ₯Ό λ§λ€ λ μμ£Ό νμ© |
μ¬λ¬ κ°λ€μ 미리 μ΄λ¦μ μ μνμ¬ μ΄κ±°ν΄ λκ³ μ¬μ©νλ νμ
μ΄κ±°, λ§€κ²¨μ§ κ°μ μ¬μ©ν΄ enum λ©€λ²μ μ΄λ¦μ μμλΌ μ μμ΅λλ€.
λ³μλ€μ νλμ κ·Έλ£ΉμΌλ‘ λ¬Άκ³ μΆμ λ μ¬μ©ν μ μμΌλ©° νΉμ κ°μ μ§ν©μ μ μνλ©° μ΄λ μ½λλ₯Ό λ κ°λ
μ± μκ³ μ μ§λ³΄μ νκΈ° μ½κ² λ§λ€μ΄μ€λλ€.
enum Color {
white = "#ffffff",
red = "#F1061A ",
orange = "#FA7804",
yellow = "#FAEB04",
green = "#3BE80D",
darkGreen = "#065823",
blue = "#2ACAF9",
navy = "#0C188F",
purple = "#6111DD",
black = "#000000",
}
let RedColor: Color = Color.red;
console.log(RedColor);
type assertionμ ν μ’
λ₯λ‘μ¨ λ¦¬ν°λ΄ νμ
μ μΆλ‘ λ²μλ₯Ό μ€μ΄κ³ κ°μ μ¬ν λΉμ λ§κΈ° μν λͺ©μ μΌλ‘ λ§λ€μ΄μ‘μ΅λλ€.
as constλ₯Ό μ¬μ©νλ©΄ μμ νμ
μ΄λ μ°Έμ‘° νμ
μ΄λ κ°μ μ¬ν λΉμ λ§μλ²λ¦¬κΈ° λλ¬Έμ μλμΉ μμ λ³κ²½μΌλ‘ μΈν μ€λ₯λ₯Ό μμ¨ μ μμ΅λλ€.
const InternationalAirport = {
Korea: "ICN",
Japan: "HND",
France: "CDG",
Vietnam: "HAN",
UK: "LCY",
Italy: "FCO",
} as const; // read only (μ½κΈ° μ μ©)
const DomesticAirport = {
incheon: "ICN",
kimpo: "GMP",
jeju: "CJU",
} as const;
const AmericaAirport = {
WashingtonDC: "IAD",
NewYork: "JFK",
LosAngeles: "LAX",
Orlando: "MCO",
Seattle: "SEA",
Chicago: "ORD",
Boston: "BOS",
Guam: "GUM",
} as const;
as constλ₯Ό λΆμ΄λ©΄ κ°μ²΄μ λͺ¨λ νλ‘νΌν° κ°λ€μ΄ readonlyμ 리ν°λ΄ κ°μ κ°μ§κ² λ©λλ€.
리ν°λ΄ νμ
μ μΆλ‘ λ²μκ° λ¦¬ν°λ΄ κ° μμ²΄λ‘ νμ λλ©΄μ μ’ λ μμ νκ² μ½λλ₯Ό μμ±ν μ μμ΅λλ€.
const Colors = {
white = "#ffffff",
red = "#F1061A ",
green = "#3BE80D",
blue = "#2ACAF9",
black = "#000000",
} as const;
enum COLOR {
white,
red,
green,
blue,
black,
}
νμ
Typeμ νλ‘νΌν° ν€μ μ§ν©μΌλ‘ νμ
μ μμ±ν΄ νμ
μ νλ‘νΌν°λ₯Ό λ€λ₯Έ νμ
μ 맀ν μν€λλ° μ¬μ©ν©λλ€.
<> μμ μ΄λ€ κ°μ΄ μ€λμ§μ λ°λΌ λ€λ₯΄κ² μ¬μ©ν μ μμ΅λλ€ :
Record<Key, Value> π key & value ν λ²μ κ°μ²΄ νμ
μΌλ‘ μ§μ ν μ μμ΅λλ€.
Record<keys, type> π ν μ νμ μμ±μ λ€λ₯Έ μ νμ 맀ννλ λ° μ¬μ©ν μ μμ΅λλ€.
type postContents = Record<string, string>;
let post: postContents = {
title: "Mobi",
contents:
"λͺ¨λΉλ κ°λΉμΌ μ¬κ΅μ‘, νμ€μ μκ° ν¬μμ κ²°μ¬μ κ²λ¨Ήμ μΈμμ λͺ¨λ λΉμ 곡μλ€μ μν΄ λ¬΄λ£λ‘ κ΅μ‘μ μ 곡νκ³ μ·¨μ
μ΄νμ μΈνλΌ νμ±μ ν΅ν΄ μλ‘μ΄ μμμ μμνλ λΉμ리 λ¨μ²΄μ
λλ€.",
};
interface Mobi {
term : number;
isOperator: boolean;
whichDeveloper: "FE" | "BE" | "Full";
}
type GraduatesName = "Zoey" | "Zero";
const graduates: Record<Mobi, Developer> = {
Zoey: { term: 1, isOperator: true, whichDeveloper: "FE" },
Zero: { term: 1, isOperator: true, whichDeveloper: "FE" },
};
<>μ νμ
μ λͺ¨λ νλ‘νΌν°λ₯Ό ?(optional)λ‘ λ§λ€μ΄ λ²λ¦¬λ νμ
μ
λλ€.
μ£Όμ΄μ§ νμ
μ λͺ¨λ νμ νμ
μ§ν©μ λνλ΄λ νμ
μ λ°νν©λλ€.
νΉμ νμ
μ λͺ¨λ μμ±μ κ°μ μ νμ μΌλ‘ λ§λ€μ΄μ€ μ μμ΅λλ€.
interface Person {
name: string;
age: number;
}
type PartialPerson = Partial<Person>;
const partialPerson: PartialPerson = {};
partialPerson.name = "Peanut";
partialPerson.age = 20;
interface Mobi {
username: string;
term: number;
sort: "FE" | "BE" | "Full";
gender: "male" | "female";
isOperator:boolean;
isDirector?:boolean;
}
let Kimi: Mobi {
username: 'Kimi';
term:2;
sort: "FE";
gender:"female";
isOperator: false;
}
let novice: Partial<Mobi> = {
name: "Peanut",
sort: "Full",
isDirector: true,
};
let newbie: Partial<Mobi> = {
name: "Amy",
sort: "FE",
term: 2;
};
νΉμ νμ
μμ μ§μ λ μμ±μ μ μΈν λλ¨Έμ§ μμ±μ κ°μ§λ μλ‘μ΄ νμ
μ μμ±νλ νμ
μ
λλ€.
Omit<T,K>λ‘ νν π Tμμ λͺ¨λ νλ‘νΌν°λ₯Ό μ νν λ€μ Kλ₯Ό μ κ±°ν νμ
μ ꡬμ±ν©λλ€.
interface Mobi {
name: string;
age: number;
isMember: boolean;
}
type MobiPreview = Omit<Mobi, "isMember">;
const mobi: MobiPreview = {
name: "Chestnut",
age: 19,
};
νΉμ νμ
μμ μ§μ λ μμ±λ§μ μ ννμ¬ μλ‘μ΄ νμ
μ μμ±ν©λλ€.
Pick<T,K>λ‘ νν π Tμμ νλ‘νΌν° Kμ μ§ν©μ μ νν΄ νμ
μ ꡬμ±ν©λλ€.
interface Mobi {
name: string;
age: number;
isMember: boolean;
}
type MobiInfo = Pick<Mobi, "name" | "age">;
const Mobi: MobiPreview = {
name: "Chestnut",
age: 19,
};
(Excludeμ λ°λλλ κ°λ
)
첫λ²μ§Έ μ λ€λ¦ νμ
Tμ λνμ¬ μ λ€λ¦ νμ
U μ€ ν λΉ κ°λ₯ν νμ
μ ν λΉν©λλ€.
Extract<T,U> π Tμμ Uμ ν λΉ ν μ μλ λͺ¨λ μμ±μ μΆμΆνμ¬ νμ
μ ꡬμ±
type Extract<T, U> = T extends U ? T : never π Tνμ
μμ Uνμ
κ³Ό κ²ΉμΉλ© νμ
λ§μ μΆμΆ
type Animal = "Cat" | "Dog" | "Pig" | "Cow";
type Pet = "Cat" | "Dog";
type myPet = Extract<Animal, Pet>;
ReturnType< T > μΌλ‘ νν π ν¨μ Tμ λ°ν νμ μΌλ‘ ꡬμ±λ νμ μ λ§λ¦
declare function Peanuts(): { name: string; age: number };
type MobiType0 = ReturnType<() => string>;
type MobiType1 = ReturnType<(s: string) => void>;
type MobiType2 = ReturnType<<Peanuts>() => Peanuts>;
type MobiType3 = ReturnType<<Peanuts extends MobiStudy, MobiStudy extends number[]>() => Peanuts>;
type MobiType4 = ReturnType<typeof Peanuts>;
type MobiType5 = ReturnType<any>;
νΉμ ν¨μμ μΆλ ₯μ λ€λ₯Έ ν¨μμμ κ°μ ΈμμΌ νλ μν©μμ λ§€μ° μ μ©νκ² μ°μΌ μ μμ΅λλ€.
sendDateμ λ¦¬ν΄ κ²°κ³Όλ₯ΌΒ Dataλ‘ νμ΄ννμ¬ μ λ¬ ν μ¬μ©νκ³ μμ΅λλ€.
function sendData(a: number, b: number) {
return {
a: `${a}`,
b: `${b}`
}
}
type Data = {
a: string,
b: string
}
function consoleData(data:Data) {
console.log(JSON.stringify(data));
}
let stringifyNumbers = sendData(1, 2);
consoleData(stringifyNumbers);
λ§μ½ sendDataμ μΆλ ₯ νμ
μ΄ λ°λλ©΄ Data νμ
λ λ°κΏμ€μΌ ν κ²μ
λλ€.
μ΄λ° μΌμ΄μ€λ₯Ό λ§κΈ° μν΄, λ κ°μ νμ
μ λκΈ°ν ν΄μ€λλ€.
function sendData(a: number, b: number) {
return {
a: `${a}`,
b: `${b}`
}
}
type Data = ReturnType<typeof sendData> // ReturnData νμ
μ sendDataμ λ°ν νμ
μ΄ μλ μ§μ
// The same as writing:
type Data = {
a: string,
b: string
}
interface SignUpData {
email: string;
password: string;
nickname: string;
name: string;
age?: number;
phone?: number;
location?: string;
}
type Mobi = {
name: string;
term: number;
isOperator?: boolean;
isDirector?:boolean;
}
type MobiOperator = {
name: string;
term: number;
isOperator: true;
}
const Jane:MobiOperator = {
name: "Jane",
term: 1,
isOperator: true,
}
μ λμ¨ νμ
κ³Ό νΈνλλ νμ
μ λ©μλλ‘ νΈμΆ μλ μ μ μ μλ¬ λ°μ X
μ νν propsμ νμ
μ μΆλ‘ , valueμ λ©μλλ₯Ό μ¬μ©ν©λλ€.
νμ
μΆλ‘ κ³Ό μ€ν κ²μ¬λ₯Ό λμμ ν μ μμ΄ κ°μ²΄μ νμ
κ²μ¬μ©μΌλ‘ μ¬μ©λ©λλ€.
const palette = {
purple: [100, 9, 240],
navy: [31, 12, 188],
blue: [12, 202, 162],
} satisfies Record<"purple" | "navy" | "blue", [number, number, number]>;
const grade = {
a: 90,
b: "B",
c: 70,
f: 60,
} satisfies Record<"a" | "b" | "c" | "f", number | string>;
Type Assertion (νμ
λ¨μΈ) π λ³μμ νμ
μ κ°λ°μκ° λͺ
μμ μΌλ‘ μ§μ νλ λ°©λ²
νμ
μ λ¨μΈνλ λ°©μμλ ν¬κ² 3κ°μ§κ° μμ΅λλ€.
TypeScriptλ νμ
μΆλ‘ μ ν΅ν΄ λλΆλΆμ μν©μμ νμ
μ μΆλ‘ ν μ μκΈ° λλ¬Έμ,
λͺ
μμ μΈ νμ
λ¨μΈμ΄ νμν κ²½μ°λ₯Ό μ μΈνκ³ λ μ¬μ©μ μ΅μννλ κ²μ΄ μ’λ€λ κ²μ μ μνλ©° μ¬μ©νλκ² μ’μ κ² κ°μ΅λλ€.
[ Type Assertion λ°©μ ]
1. <> π JSX λ¬Έλ²μ΄λ κ²Ήμ³μ μμ£Ό μ¬μ© X
2. as
3. satisfies
type annotation π νμ
μ μ£Όμμ λ€λ λ°©λ²
Annotateλ "μ£Όμμ λ¬λ€" λΌλ μ¬μ μ μΈ λ»μ κ°μ§κ³ μμ΅λλ€.
νμ
μ€ν¬λ¦½νΈμμλ λ³μ, ν¨μ, ν¨μ λ°νκ°μ λ°μ΄ν° νμ
μ μ§μ νκΈ° μν΄ "νμ
μ΄λ
Έν
μ΄μ
"μ μ¬μ©ν©λλ€.
ν λ² μλ³μλ₯Ό νΉμ νμ
μΌλ‘ annotated νλ©΄ ν΄λΉ νμ
λ§ μ¬μ©ν μ μκ² λ©λλ€.
λ€λ₯Έ νμ
μ μ¬μ©νκ² λλ©΄ νμ
μ€ν¬λ¦½νΈ μ»΄νμΌλ¬κ° μλ¬λ₯Ό λμ§λλ€.
// λ³μμ νμ
μ§μ νκΈ°
let a:number;
a = "string"; //error
a = 20; // o
function greetings(a: number){
console.log(a)
}
hello(10) // o
function hello(a: number){
console.log(a)
}
hello("world") //error
1οΈβ£ κ°λ¨ν νμ
μ’νκΈ°
μ΄λ
Έν
μ΄μ
μ§μ , typeof 쑰건문μ μ¬μ©ν΄ κ°λ¨ν νμ
μ μ’ν μ μμ΅λλ€.
type Colors = "red" | "green" | "blue";
type RGB = [red: number, green: number, blue: number];
const paletteRGB: Record<Colors, string | RGB> = {
red: [255, 0, 0],
green: "#00ff00",
blue: [0, 0, 255],
};
paletteRGB.red.map(0); // β π νμ
μ΄ stringμ΄ μ¬μ§ RGBκ° μ¬μ§ νμ μ§μ μ μκΈ° λλ¬Έμ map μ¬μ© λΆκ°
paletteRGB.green.toUpperCase(); // β π νμ
μ΄ stringμ΄ μ¬μ§ RGBκ° μ¬μ§ νμ μ§μ μ μκΈ° λλ¬Έμ toUpperCase μ¬μ© λΆκ°
// λ°λΌμ typeof 쑰건문 νμ©
if (typeof paletteRGB.green === "string") {
paletteRGB.green.toUpperCase();
}
λ§€λ² μ‘°κ±΄λ¬Έμ μ°κΈ° λ²κ±°λ‘λ€λ©΄ satisfies μ¬μ©ν΄μ ν΄μν μ μμ΅λλ€.
// satisfies μ¬μ©μΌλ‘ νμ
μ μ’ν 쑰건문 μμ΄ ν΄λΉ νμ
μ λ§λ λ©μλ μ¬μ© κ°λ₯
const paletteRGB = {
red: [255, 0, 0],
green: "#00ff00",
blue: [0, 0, 255],
} satisfies Record<Colors, string | RGB>;
paletteRGB.green.toUpperCase();
2οΈβ£ κ°κ²°ν νμ
체ν¬
2-1. μ΄λ
Έν
μ΄μ
μΌλ‘ νμ λ
// νμ
μ§μ
interface Data {
a: "A" | "B" | "C" | "D";
b: number;
c: string;
d: string[];
}
// Partialλ‘ νμ
μ μ νμ μΌλ‘ κ°μ Έμ¨ baseData1
const smallData: Partial<Data> = {
//Partial<Data>
a: "A",
b: 1,
};
const smallData: Data = {
...smallData, // smallDataμ νμ
μ a,bκ° μλλΌ Partialνμ
μΌλ‘ νλ¨νκΈ° λλ¬Έμ μ¬μ© λΆκ°λ₯
c: "string",
d: ["array"],
};
2-2. satisfies μ¬μ©
const smallData2 = {
a: "A",
b: 1,
} satisfies Partial<Data>;
const smallData2: Data = {
...smallData2, // satisfiesμ¬μ©μΌλ‘ Data νμ
μ a,bλ₯Ό λ§μ‘±νκΈ° λλ¬Έμ μ¬μ© κ°λ₯
c: "string",
d: ["array"],
};
3οΈβ£ μμ ν νμ
λ¨μΈ
3-1. asλ₯Ό μΌμ λ
interface Developer {
name: string;
nickname: string;
major: "frontend" | "backend";
}
// μλ¬κ° λ μν©μΈλ° as λλ¬Έμ κ·Έλ₯ ν΅κ³Όλλ€
// as : νμ
μ μ κ²νμ§ μμ
const dev = {
name: "Peter",
nickname: "Peanut",
} as User;
3-2. satisfies μ¬μ©
const dev = {
name: "Peter",
nickname: "Peanut",
} satisfies Developer as Developer; // μλ¬ λ°μμμΌ μ€
// satisfiesμ¬μ©μΌλ‘ νμ
μ΄ λ§μ‘±νλμ§ νμΈν λ€μ asλ‘ λ¨μΈνκΈ° λλ¬Έμ μμ νκ² νμ
μ λ¨μΈ ν μ μμ
μ¬μ¬μ©μ±μ΄ λμ μ»΄ν¬λνΈλ₯Ό λ§λ€ λ μμ£Ό νμ©λ©λλ€.
νΉν, νκ°μ§ νμ
λ³΄λ€ μ¬λ¬ κ°μ§ νμ
μμ λμνλ μ»΄ν¬λνΈλ₯Ό μμ±νλλ° λ§μ΄ μ¬μ©ν©λλ€.
T κΊΎμ μμ λ£λ νμ μΌλ‘ λͺ¨λ Tκ° ν΄λΉ νμ μΌλ‘ λ°λκ² λ©λλ€.
// μ λ€λ¦ νμ
μ κΈ°λ³ΈμΌλ‘ μ°λ λ°©λ²
type MyProfile<T> = {
hobby: Array<T>;
};
// extends generic type
type StudentType = {
fullName: string;
nickName: string;
age: number;
};
const StudentDetail = <T extends StudentType>(detail: T) => ({
fullName: "TopDragon",
nickName: "Peanut",
age: 20,
isDirector: true,
});
references
return type >> https://itchallenger.tistory.com/638