Categories
Tags
Ai 生成 API学习 API简化 api请求 API调用 best-practices Blogging Caching catchTag catchTags class CLI Config context Context Context.Tag CSS Customization Demo development DocC Docker dual API Effect effect Effect.Service Effect.succeed Example extension ffmpeg filterOrFail flatMap Fuwari gen generator grep hooks HTML HTTP响应 IDE自动补全 iOS javascript JavaScript Javascript Layer.effect Layer.provide Layers Linux Markdown Mock n8n Next.js ParseError pipe pokemon PostCSS process.env progress Promise promise provideService PWA react React React Hook Form React Query React Router react-native Scheduler Schema Schema.Class security Service Worker Services SSR state-management suspense Tagged Errors TaggedError TanStack Query TanStack Start tips tryPromise tsconfig TypeScript typescript Video VS Code vscode Web API Web Development yield yt-dlp Zod 不透明类型 二叉树 代码组织 任务调度 优先级 使用服务 依赖注入 依赖管理 值语义 入门教程 最佳实践 最小堆 函数式编程 函数组合 前端 前端开发 副作用 副作用控制 可视化 可组合性 可维护性 可访问性 命令行 响应过滤 多个错误 实现 实践指南 层 层依赖 层组合 工具链 并发控制 应用架构 延迟执行 开发技巧 开发教程 开源 异步处理 异步操作 异步编程 性能优化 手写系列 排序 接口设计 插件开发 数据结构 数据获取 数据解码 数据验证 无限滚动 日历 日志分析 服务 服务依赖 服务定义 服务实现 服务提供 测试 源码分析 状态管理 环境变量 生成器 离线支持 程序分离 算法 类型安全 类型定义 类型推断 类型系统 类定义 线性代码 组合 翻译 自动化 自定义错误 表单验证 记忆化 设计模式 语义化 运维 运行时验证 部分应用 配置 配置变量 配置服务 配置管理 重构 错误处理 错误定义 错误恢复 项目设置
725 words
4 minutes
TypeScript 类型检查配置
本文将详细介绍 TypeScript 中与类型检查相关的编译器选项,这些选项决定了类型系统的严格程度和行为方式。
strict - 严格模式
启用所有严格类型检查选项。
{
"compilerOptions": {
"strict": true
}
}启用 strict 相当于同时启用以下所有选项:
alwaysStrictstrictNullChecksstrictBindCallApplystrictFunctionTypesstrictPropertyInitializationnoImplicitAnynoImplicitThisuseUnknownInCatchVariables
建议在新项目中启用此选项,以获得最佳的类型安全性。
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/