TypeScript strict 모드를 켜면 에러가 너무 많이 나는데 하나씩 설명해주세요.
strict 모드 주요 옵션:
// tsconfig.json{ "compilerOptions": { "strict": true, // 아래 모두 활성화 // "noImplicitAny": true, // 타입 미지정 시 에러 // "strictNullChecks": true, // null/undefined 체크 필수 // "strictFunctionTypes": true, // 함수 타입 엄격 체크 }}
가장 많이 만나는 에러:
// noImplicitAnyfunction add(a, b) {} // ❌function add(a: number, b: number) {} // ✅// strictNullChecksconst el = document.getElementById("app"); // HTMLElement | nullel.textContent = "hi"; // ❌ el이 null일 수 있음el?.textContent = "hi"; // ✅ 또는 if (el) 체크
처음엔 번거롭지만 strict 모드가 런타임 버그를 크게 줄여줍니다. 새 프로젝트는 반드시 strict: true로 시작하세요.
한번에 켜지 말고 단계별로:
noImplicitAny
// @ts-ignore
strictNullChecks
// @ts-strict
또는 typescript-strict-plugin으로 새 파일만 strict 적용하는 전략도 있습니다.
typescript-strict-plugin
댓글 3
strict 모드 주요 옵션:
가장 많이 만나는 에러:
처음엔 번거롭지만 strict 모드가 런타임 버그를 크게 줄여줍니다. 새 프로젝트는 반드시 strict: true로 시작하세요.
한번에 켜지 말고 단계별로:
noImplicitAny만 먼저 켜기// @ts-ignore로 임시 무시 (나중에 수정)strictNullChecks다음// @ts-strict적용하는 방법도 있음또는
typescript-strict-plugin으로 새 파일만 strict 적용하는 전략도 있습니다.