const knownValue = seasonCounts.get("I Love Lucy")!;
console.log(knownValue.toUpperCase()); // 타입 오류는 아니지만, 런타임 오류가 발생함
// Runtime TypeError: Cannot read property 'toUpperCase' of undefined.// Runtime TypeError: Cannot read property 'toUpperCase' of undefined.
타입 어서션은 자주 사용하면 안 되고, 사용하는 것이 안전하다고 확실히 확신할 때만 사용해
야 합니다.
어서션
vs
. 선언
변수 타입을 선언하기 위해 타입 애너테이션을 사용하는 것과 초깃값으로 변수 타입을 변경하
기 위해 타입 어서션을 사용하는 것 사이에는 차이가 있습니다. 변수의 타입 애너테이션과 초
깃값이 모두 있을 때, 타입스크립트의 타입 검사기는 변수의 타입 애너테이션에 대한 변수의
초깃값에 대해 할당 가능성 검사를 수행합니다. 그러나 타입 어서션은 타입스크립트에 타입 검
사 중 일부를 건너뛰도록 명시적으로 지시합니다.
다음은 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.