725 words
4 minutes
TypeScript 类型检查配置
2025-02-26 10:41:46
2025-12-24 23:45:46

← 返回 TSConfig 参考指南


本文将详细介绍 TypeScript 中与类型检查相关的编译器选项,这些选项决定了类型系统的严格程度和行为方式。

strict - 严格模式#

启用所有严格类型检查选项。

{
  "compilerOptions": {
    "strict": true
  }
}

启用 strict 相当于同时启用以下所有选项:

  • alwaysStrict
  • strictNullChecks
  • strictBindCallApply
  • strictFunctionTypes
  • strictPropertyInitialization
  • noImplicitAny
  • noImplicitThis
  • useUnknownInCatchVariables

建议在新项目中启用此选项,以获得最佳的类型安全性。

strictNullChecks - 严格空值检查#

在严格的空值检查模式下,nullundefined 不再被视为其他类型的有效值。

{
  "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
  }
}

主要影响:

  1. 检查函数参数的双变性
  2. 更严格的方法重载检查
  3. 更准确的 this 类型推断

strictBindCallApply - 严格绑定调用#

bindcallapply 方法进行更严格的类型检查。

{
  "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
  }
}

注意事项#

  1. 逐步启用严格选项

    • 在现有项目中,建议逐步启用这些选项
    • 先修复 noImplicitAnystrictNullChecks 的问题
    • 然后再启用其他严格选项
  2. 使用类型断言

    • 在必要时可以使用类型断言绕过严格检查
    • 但应该谨慎使用,并记录原因
  3. 项目规模考虑

    • 大型项目可能需要更严格的配置
    • 小型项目可以根据需求选择性启用
TypeScript 类型检查配置
https://0bipinnata0.my/posts/typescript/tsconfig/01-type-checking/
Author
0bipinnata0
Published at
2025-02-26 10:41:46