Categories
Tags
Ai 生成 best-practices Blogging Caching CLI context CSS Customization Demo development DocC Example extension Fuwari generator grep hooks HTML iOS JavaScript javascript Javascript Linux Markdown Next.js PostCSS progress Promise PWA react React React Hook Form React Query React Router react-native Scheduler security Service Worker SSR state-management suspense TanStack Query TanStack Start tips tsconfig TypeScript typescript Video VS Code vscode Web API Web Development Zod 二叉树 代码组织 任务调度 优先级 入门教程 最佳实践 最小堆 前端 前端开发 可视化 可访问性 命令行 工具链 并发控制 开发教程 开源 异步处理 异步编程 性能优化 手写系列 排序 插件开发 数据结构 数据获取 无限滚动 日历 日志分析 源码分析 状态管理 离线支持 算法 翻译 表单验证 设计模式 语义化 运维 配置
725 words
4 minutes
TypeScript 类型检查配置
本文将详细介绍 TypeScript 中与类型检查相关的编译器选项,这些选项决定了类型系统的严格程度和行为方式。
strict - 严格模式
启用所有严格类型检查选项。
{ "compilerOptions": { "strict": true } }
启用 strict
相当于同时启用以下所有选项:
alwaysStrict
strictNullChecks
strictBindCallApply
strictFunctionTypes
strictPropertyInitialization
noImplicitAny
noImplicitThis
useUnknownInCatchVariables
建议在新项目中启用此选项,以获得最佳的类型安全性。
strictNullChecks - 严格空值检查
在严格的空值检查模式下,null
和 undefined
不再被视为其他类型的有效值。
{ "compilerOptions": { "strictNullChecks": true } }
示例:
// strictNullChecks: false let name: string = null; // 允许 // strictNullChecks: true let name: string = null; // 错误 let nullableName: string | null = null; // 正确
noImplicitAny - 禁止隐式 any
不允许隐式的 any
类型。
{ "compilerOptions": { "noImplicitAny": true } }
示例:
// noImplicitAny: false function fn(s) { // 参数 s 隐式具有 any 类型 console.log(s.subtr(3)); } // noImplicitAny: true function fn(s: string) { // 必须显式指定类型 console.log(s.substr(3)); }
strictFunctionTypes - 严格函数类型
启用对函数类型的更严格检查。
{ "compilerOptions": { "strictFunctionTypes": true } }
主要影响:
- 检查函数参数的双变性
- 更严格的方法重载检查
- 更准确的 this 类型推断
strictBindCallApply - 严格绑定调用
对 bind
、call
和 apply
方法进行更严格的类型检查。
{ "compilerOptions": { "strictBindCallApply": true } }
示例:
function fn(x: string) { return parseInt(x); } // strictBindCallApply: true const n = fn.call(undefined, 10); // 错误:参数类型不匹配 const m = fn.call(undefined, "10"); // 正确
strictPropertyInitialization - 严格属性初始化
确保类的非可选属性在构造函数中被初始化。
{ "compilerOptions": { "strictPropertyInitialization": true } }
示例:
class User { name: string; // 错误:属性未初始化 age: number = 0; // 正确:已初始化 email!: string; // 正确:使用 ! 断言 constructor() {} }
noImplicitThis - 禁止隐式 this
不允许 this
有隐式的 any
类型。
{ "compilerOptions": { "noImplicitThis": true } }
示例:
function callback(this: void) { // 正确:显式指定 this 类型 console.log(this); } function problem() { // 错误:隐式 this 类型 console.log(this.value); }
useUnknownInCatchVariables - catch 变量使用 unknown
在 catch 子句中使用 unknown
而不是 any
。
{ "compilerOptions": { "useUnknownInCatchVariables": true } }
示例:
try { // 某些可能抛出错误的代码 } catch (e) { // e 的类型为 unknown if (e instanceof Error) { console.log(e.message); // 正确:已经进行了类型检查 } }
最佳实践配置
以下是推荐的类型检查配置:
{ "compilerOptions": { "strict": true, "noImplicitAny": true, "strictNullChecks": true, "strictFunctionTypes": true, "strictBindCallApply": true, "strictPropertyInitialization": true, "noImplicitThis": true, "useUnknownInCatchVariables": true } }
注意事项
逐步启用严格选项
- 在现有项目中,建议逐步启用这些选项
- 先修复
noImplicitAny
和strictNullChecks
的问题 - 然后再启用其他严格选项
使用类型断言
- 在必要时可以使用类型断言绕过严格检查
- 但应该谨慎使用,并记录原因
项目规模考虑
- 大型项目可能需要更严格的配置
- 小型项目可以根据需求选择性启用
TypeScript 类型检查配置
https://0bipinnata0.my/posts/typescript/tsconfig/01-type-checking/