νμ ꡬμ±π§κ³Ό νμ μ μπ‘ μ λνμ¬
π νμ μ€ν¬λ¦½νΈλ₯Ό μ¬μ©νλ μ΄μ
- μ»΄νμΌ λ¨κ³μμ μλ¬λ₯Ό νμΈ ν μ μλ€.
- βοΈλμ μΈ μλ°μ€ν¬λ¦½νΈλ₯Ό μ μ μΌλ‘ μ¬μ© ν μ μλ€.
- βοΈλλ²κΉ μ΄ μ¬μμ§
- IDE μλμμ± κΈ°λ₯
- βοΈμμ μκ°μ λ¨μΆ μν¬ μ μλ€.
β
π νμ μ μνκΈ°
Const test = {
id:0,
name: βMerryβ,
kind: βdogβ
}
μ΄λ° λ³μκ° μλ€κ³ κ°μ ν΄λ³΄μ. π€
μ΄ λ³μλ id μμμ stringμ΄ λ€μ΄κ° μλ μκ³ numberκ° λ€μ΄κ° μλ μλ€.
id μ number λ§ λ€μ΄κ° μ μκ² μ€μ ν΄μ£Όκ³ μΆλ€λ©΄ νμ
μ€ν¬λ¦½νΈλ₯Ό μ΄μ©ν΄ νμ
μ μ μν΄μ£Όλ©΄ λλ€ π
κ°μ²΄μ ννλ₯Ό λͺ
μμ μΌλ‘ λνλ΄κΈ° μν΄μ interfaceλ₯Ό μ¬μ©νλ€.
Interface λμ classλ₯Ό μ¬μ©ν μλ μλ€. νμ§λ§ interfaceλ₯Ό νμ©νλλ‘ νμβΌοΈ
Interface Test {
id: number;
name: string;
kind: string;
}
interfaceλ‘ κ°μ²΄μ ννλ₯Ό μ§μ ν΄μ£Όμλ€.
Class TestClass {
id: number;
name: string;
kind: string;
constructor(id: number, name: string, kind: string){
this.id = id;
this.name = name;
this.kind = kind;
}
}
classλ‘ μ μΈν κ²½μ°μ μ΄λ κ² νμ©ν μ μλ€.
λ³μ μ μΈ λ€μ interfaceλ‘ μ μΈν νμ
λͺ
μ μ μ΄μ£Όλ©΄ λλ€.
Const test: Test = {
id:0,
name: βMerryβ,
kind: βdogβ
}
νμ ꡬμ±
- μ λμ¨ (Unions)
μ λμ¨μ μλ°μ€ν¬λ¦½νΈμ or μ°μ°μ (||) μ κ°μ΄ βAμ΄κ±°λ Bμ΄λ€β λ μλ―Έμ΄λ€.
νμ μ μ¬λ¬κ° μ§μ ν΄λκ³ κ·Έ μ€ νλμΌ μ μμμ μ μΈνλ€.
type Kind = βdogβ | βcatβ | βetcβ;
β
TIPπ
Typeof λ‘ λ³μμ νμ
μ κ²μ¬ν μ μλ€.
κ²μ¬ ν μ μλ νμ
μ μλμ κ°λ€.
String, boolean, undefined, number, function
β
- μ λ€λ¦ (Generics)
μ λ€λ¦μ μ¬λ¬ λ°μ΄ν° νμ μ λν΄ λμΌνκ² λμν μ μκ² ν΄μ£Όλ κΈ°λ₯μ΄λ€.
μ£Όλ‘ μ¬λ¬ κ°μ§ νμ μμ λμνλ μ»΄ν¬λνΈλ₯Ό μμ±νλλ° μ¬μ©νλ€.
Function Test( text ){
return text;
}
Test(βstringβ);
Test(5);
Test(true);
μ΄λ€ κ°μ λ겨μ€λ κ·Έλλ‘ λ°ννλ€.
κ°μ νμ
μ μ§μ ν΄μ€λ€λ©΄ λ€μκ³Ό κ°μ΄ μ½λλ₯Ό μ§μΌνλ€.
Function TestString( text: string ): string{
return text;
}
Function TestNumber( text: number ): number{
return text;
}
Function TestBoolean( text: boolean ): boolean{
return text;
}
μ½λλ λμΌνλ λ°μ΄ν° νμ
μ΄ λ€λ₯΄κΈ°μ νμ
μ λ°λ₯Έ ν¨μλ₯Ό κ°κ° λ§λ€μ΄μ£Όμλ€.
μ°Έ λΉν¨μ¨μ μΈ μ½λκ° μλ μ μλ€. π€ μ΄λ΄ λ μ λ€λ¦μ μ¬μ©νλ©΄ νΈνλ€.
μ λ€λ¦μ μ¬μ©ν΄μ λ°μ΄ν°λ₯Ό λ겨보λλ‘ νμ.
β
Function Test<T>( text: T ): T{
return text;
}
Test<string>(βstringβ);
Test<number>(5);
Test<boolean>(true);
μ λ€λ¦μ μ΄λ° λ°©μμΌλ‘ μ¬μ©νλ©΄ νΈνλ€ βοΈ