📎아이템 2 타입스크립트 설정 이해하기
function add(a, b) {
return a + b;
}
add(10, null);{
"compilerOptions": {
"noImplicitAny": true
}
}📍타입스크립트 설정
noImplicitAny
noImplicitAnystrictNullChecks
strictNullChecks📍요약
Last updated
function add(a, b) {
return a + b;
}
add(10, null);{
"compilerOptions": {
"noImplicitAny": true
}
}noImplicitAnystrictNullChecksLast updated
// noImplicitAny가 해제되어 있을 때에는 유효하다.
function add(a, b) {
return a + b;
}function add(a, b) {
// ~ Parameter 'a' implicitly has an 'any' type
// ~ Parameter 'b' implicitly has an 'any' type
return a + b;
}function add(a: number, b: number) {
return a + b;
}// strictNullChecks 가 해제되었을 때 유효한 코드이다.
const x: number = null; // OK, null is a valid numberconst x: number = null;
// ~ Type 'null' is not assignable to type 'number'
// null 대신 undefined를 써도 같은 오류가 난다.const x: number | null = null;const statusEl = document.getElementById('status');
statusEl.textContent = 'Ready';
// ~~~~~ 'statusEl' is possibly 'null'.
if (statusEl) {
statusEl.textContent = 'Ready'; // OK, null has been excluded
}
statusEl!.textContent = 'Ready'; // OK, we've asserted that el is non-null