[Typescript] 변수 뒤 느낌표

민병대·2023년 5월 8일
0

Definite Assignment Assertions

example

let x: number;
initialize();
console.log(x + x);
//          ~   ~
// Error! Variable 'x' is used before being assigned.
function initialize() {
  x = 10;
}
// Notice the '!'
let x!: number;
initialize();
// No error!
console.log(x + x);
function initialize() {
  x = 10;
}

변수의 값이 있다고 확신할 때 사용하여
에러를 제거할 수 있다

profile
마케터 출신 개발자

0개의 댓글