νΉμ κ°λ€μ μ§ν©μ μλ―Ένλ μλ£ν
β μ«μν μ΄λ
enum Shoes{
Nike, // 0
Adidas // 1
}
let myShoes = Shoes.Nike;
console.log(myShoes); // 0
- λ³λμ κ°μ μ§μ νμ§ μμΌλ©΄ μ«μνμΌλ‘ μ·¨κΈ ν©λλ€.
enum Direction {
Up = 1,
Down,
Left,
Right
}
// κ²°κ³Ό
Up - 1
Down - 2
Left - 3
Right - 4
β λ¬Έμν μ΄λ
enum Shoes{
Nike = 'λμ΄ν€',
Adidas = 'μλλ€μ€'
}
let myShoes = Shoes.Nike;
console.log(myShoes);// 'λμ΄ν€'
μμ
β μ¬μ©νκΈ° μ
function askQuestion(answer: string){
if(answer === 'yes'){
console.log('μ λ΅μ
λλ€');
}
if(answer === 'no'){
console.log('μ€λ΅μ
λλ€');
}
}
askQuestion('yes');
askQuestion('μμ€');
askQuestion('y');
- νλΌλ―Έν°λ₯Ό λ¬Έμμ΄λ‘ μ€μ νκΈ° λλ¬Έμ 'yes' λΌλ μλ―Έλ₯Ό κ°μ§ λ¬Έμμ΄μ λ£μ μ μμ΅λλ€.
β enum μ¬μ©
enum Answer {
Yes = 'Y',
No = 'N'
}
// answer μ νμ
μ string -> Answer μ΄λμΌλ‘ λ³κ²½
function askQuestion(answer: Answer){
if(answer === Answer.Yes){
console.log('μ λ΅μ
λλ€');
}
if(answer === Answer.No){
console.log('μ€λ΅μ
λλ€');
}
}
askQuestion(Answer.Yes);
askQuestion('Yes'); // μλ¬ λ°μ, μ΄λμ μ΄μ©ν΄μ μ μνλ©΄ μ΄λμμ μ 곡νλ λ°μ΄ν°λ§ μ¬μ© κ°λ₯
- λͺ©λ‘μ΄ νμν ννμμ
enum
μ μ μν΄μ μ¬μ©νλ κ²μ΄ μ νν μ½λ, μμΈ μ²λ¦¬μ μΌμ΄μ€λ₯Ό μ€μΌ μ μμ΅λλ€.
μ°Έκ³
'π Front-End > TypeScript' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
μ λ€λ¦(Generics) (0) | 2022.10.23 |
---|---|
νμ μ€ν¬λ¦½νΈμ ν΄λμ€ (0) | 2022.10.23 |
Union Type ( μ λμ¨ νμ ) κ³Ό Intersection Type (0) | 2022.10.22 |
Type Aliases ( νμ λ³μΉ ) (0) | 2022.10.22 |
Interface ( μΈν°νμ΄μ€ ) (0) | 2022.10.22 |