strictNullCheck 옵션은 true
로 해라.
null
, undefined
로 인해 혈압오르고 싶지 않으면
사실 더 엄격하게 쓰려면 그냥
{
"compilerOptions" : {
"strict": true"
}
}
이걸로 자동적으로 strict*
옵션이 활성화 되기 때문에 문제없다.
type TNameType = string
const names: TNameType[] = ['falcon', 'm-falcon']
// foundName 의 타입 추론은 어떻게 될까? => string
const foundName = names.find(name => name === 'vladimir')
foundName.concat()
⚠️ Runtime Error 가 발생한다.
"Cannot read properties of undefined (reading 'concat')"
{
"compilerOptions": {
"module": "CommonJS",
"target": "ES2022",
"outDir": "dist",
"baseUrl": "/",
"resolveJsonModule": true,
"strictNullChecks": true, // ✅ 무조건 하자.
"noImplicitAny": false,
"strictBindCallApply": false,
"strictPropertyInitialization": true,
}
}
type TNameType = string
const names: TNameType[] = ['falcon', 'm-falcon']
// foundName 의 타입 추론은 어떻게 될까? => string | undefined
const foundName = names.find(name => name === 'vladimir')
foundName.concat('df')
컴파일 타임에 에러를 미리 잡을 수 있다.
"TS18048: foundName is possibly undefined"