| name | typescript-strict-mode |
| description | null |
Strict Mode
Category: typescript · Status: 🟢 Active
When to use
Khi cấu hình project hoặc viết code TypeScript cần an toàn kiểu tối đa.
Steps
- Bật
"strict": true trong tsconfig.json (gồm strictNullChecks, noImplicitAny...).
- Xử lý
null/undefined tường minh: optional chaining, nullish coalescing, guard.
- Không tắt check bằng
// @ts-ignore hay as any; sửa gốc kiểu thay vì che.
- Dùng
unknown thay any cho input chưa rõ, rồi narrow trước khi dùng.
- Bật thêm
noUncheckedIndexedAccess khi truy cập mảng/object động.
Template
{ "compilerOptions": { "strict": true, "noUncheckedIndexedAccess": true } }
function getName(u: User | null): string {
return u?.name ?? "Khách";
}
Example
Good: u?.name ?? "Khách", narrow unknown bằng typeof trước khi dùng.
Avoid: (u as any).name, // @ts-ignore để bỏ qua lỗi null thay vì sửa.
Checklist