[TypeScript] Type Assertions & Type Inference


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라고 ν™•μ‹  ν•  λ•Œ μ‚¬μš©