Typescript未检测到声明“ d.ts”文件中不存在的类型名称的错误

问题描述

在这个Typescript游乐场的example中,我试图在名称空间中使用不存在的类型,但出现错误

enter image description here

enter image description here

这是预期的。

但是在我的本地开发环境中,Typescript基本上接受任何不存在的类型作为any

注意:这仅在d.ts文件内发生。

enter image description here

enter image description here

不知道这是否重要,但是我正在使用noImplicitAny: true标志。

查看我的tsconfig.json文件

{
  "compilerOptions": {
    "allowJs": true,"baseUrl": ".","esModuleInterop": true,"jsx": "react","module": "Commonjs","moduleResolution": "Node","noEmit": true,"noImplicitAny": true,"noImplicitReturns": true,"resolveJsonModule": true,"skipLibCheck": true,"sourceMap": true,"strictnullchecks": true,"target": "ES6","paths": {
      "@src/*": ["./src/*"],}
  },"include": [
    "src/**/*","functions/src/**/*","functions/index.ts"
  ],}

如何让Typescript检测那些错误

解决方法

声明文件(带有.d.ts的文件)由--skipLibCheck编译器选项覆盖。

通过指定

{
  "compilerOptions": {
    "skipLibCheck": true
  }
}

在您的tsconfig.json中,您告诉TypeScript不要验证此文件。

从广义上讲,您有两种选择来强制对此文件进行验证。

  1. 将文件从.d.ts重命名为.ts。这是最直接的方法,并且侵入性最小。仅包含声明的.ts文件是完全有效的。

  2. 删除上面的"skipLibCheck": true配置,从而在.d.ts文件中强制执行类型检查,这是默认行为。