在 TypeScript 中禁用 node_modules 的类型检查

要在 TypeScript 中禁用 node_modules 的类型检查,请在 tsconfig.json 文件中将 skipLibCheck 属性设置为 true 并确保我们使用的是最新版本的 TypeScript。 skipLibCheck 设置将跳过所有声明 (.d.ts) 文件的类型检查。

打开 tsconfig.json 文件并将 skipLibCheck 属性设置为 true。

{
  "compilerOptions": {
    "skipLibCheck": true,
    // ... other settings
  }
}

skipLibCheck 选项指示 TypeScript 编译器跳过声明 (.d.ts) 文件的类型检查。

我们可以通过在使用命令行时传递 –skipLibCheck 标志来获得相同的结果。

$ tsc --skipLibCheck

如果错误未解决,请确保 tsconfig.json 文件中的排除数组包含 node_modules 目录的路径。

{
  "compilerOptions": {
    "skipLibCheck": true,
    // ... other settings
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules"]
}

即使该设置禁用了 d.ts 文件的类型检查,TypeScript 仍会对我们在应用程序源代码中具体引用的代码进行类型检查。