问题描述
在这个Typescript游乐场的example中,我试图在名称空间中使用不存在的类型,但出现错误:
这是预期的。
但是在我的本地开发环境中,Typescript基本上接受任何不存在的类型作为any
。
注意:这仅在d.ts
个文件内发生。
不知道这是否重要,但是我正在使用noImplicitAny: true
标志。
{
"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不要验证此文件。
从广义上讲,您有两种选择来强制对此文件进行验证。
-
将文件从
.d.ts
重命名为.ts
。这是最直接的方法,并且侵入性最小。仅包含声明的.ts
文件是完全有效的。 -
删除上面的
"skipLibCheck": true
配置,从而在.d.ts
文件中强制执行类型检查,这是默认行为。