[TypeScript] Type Assertions & Type Inference
μΉ΄ν
κ³ λ¦¬ : νλ‘κ·Έλλ° μΈμ΄
νκ·Έ: ts · typescript · Type Assertions · Type Inference · νμ μΆλ‘ · νμ νλͺ
νκ·Έ: ts · typescript · Type Assertions · Type Inference · νμ μΆλ‘ · νμ νλͺ
- 1. Type Inference : νμ μΆλ‘ (λ³΄ν΅ λͺ κ°μ ννμ(μ½λ)μ λ°νμΌλ‘ νμ μ μΆλ‘ )
- 9. Type Assertions : νμ νλͺ
1. Type Inference : νμ μΆλ‘ (λ³΄ν΅ λͺ κ°μ ννμ(μ½λ)μ λ°νμΌλ‘ νμ μ μΆλ‘ )
function add(x:number, y:number):number{
return x+y;
}
const result = add(1,2);
console.log(result);
x,yλ number νμ
μΌλ‘ κ°μ£Όν¨
9. Type Assertions : νμ νλͺ
- 100% νμ μ μ₯λ΄ν λλ§ μ°μ (μμμ)
- JavaScript μ½λλ₯Ό TypeScriptμΌλ‘ ν¬ν ν λ λ§μ΄ μ
function findNumbers(): number[] | undefined {
return undefined;
}
const numbers = findNumbers();
numbers!.push(2); // !λ numberλΌκ³ νμ ν λ μ¬μ©