- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我通常设置我的 tsconfig.json
并将 strict
设置为 true
。这意味着 noImplicitAny
也设置为 true
。但是,当设置了 strict
时,typescript 似乎忽略了我在本地创建的 *.d.ts
文件的 typeRoots
条目。这是我使用的示例 tsconfig.json
:
{
"compilerOptions": {
"declaration": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [ "es2017" ],
"module": "CommonJS",
"noImplicitReturns": true,
"outDir": "lib",
"removeComments": true,
"sourceMap": true,
"strict": true,
"target": "es2017",
"typeRoots": [ "./typings", "./node_modules/@types" ],
"types": [ "node" ],
},
"compileOnSave": true,
"include": [ "./src/**/*" ]
}
我可以将以下内容添加到上面的 tsconfig.json
中,它会起作用:
{
"compilerOptions": {
...
"noImplicitAny": false,
...
}
}
这是我创建的一个示例项目来说明这个问题:
https://github.com/marcoslin/tstyping-test
知道为什么会这样吗?
最佳答案
noImplicitAny: false
并没有真正解决问题,它只是忽略了它。 template
将被隐式键入为 any
,因为未找到任何类型。你没有得到任何错误,但也没有类型安全。
真正的问题是您指定了 "types": [ "node"],
这意味着只有节点模块的类型来自 typeRoots
。参见 docs .
最简单的解决方案是从 tsconfig.json
中删除 types
元素。这个 tsconfig 没有给出任何错误:
{
"compilerOptions": {
"declaration": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [ "es2017" ],
"module": "CommonJS",
"noImplicitReturns": true,
"outDir": "lib",
"removeComments": true,
"sourceMap": true,
"strict": true,
"target": "es2017",
"typeRoots": [ "./typings", "./node_modules/@types" ]
},
"compileOnSave": true,
"include": [ "./src/**/*" ]
}
关于设置 `typeRoots` 时 typescript 忽略 `noImplicitAny`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54289791/
使用 typescript playground在打开 noImplicitAny 的情况下,我输入以下代码: async function a () { const b = JSON.p
在下面的代码片段中,尽管有一个隐式类型为 any 的参数,但 typescript 不会发出任何错误。 . declare function constrainedHOF any>(callback:
TypeScript 文档将 noImplicitAny 编译器标志记录为 Raise error on expressions and declarations with an implied an
即使我启用了 --noImplicitAny 标志,此代码也不会在 TypeScript playground 或 tsc 中向我显示任何错误。为什么不?如果我将 methodName 悬停在 pla
在我的根文件夹中,我有如下所示的 tsconfig.json 文件: { "compilerOptions": { /* Basic Options */ "target": "e
我有很多代码要移植到缺少类型的 Typescript。现在,我希望我的 IDE (WebStorm) 只突出显示需要输入的地方,我会慢慢修复它们。 如 tsconfig.json有一个 warning
我已将“noImplicitAny”和“noImplicitReturns”添加到我的 Typescript tsconfig.json 文件中: { "compilerOptions": {
我有密码 let z; z = 50; z = 'z'; 我的 tsconfig.json 是: { "compilerOptions": { "target": "es5", "
我想在编译器中启用 noImplicitAny 标志。我的问题是我使用 lodash/fp 并且到目前为止还没有打字。 因此编译器提示缺少 lodash/fp 的定义文件。 有没有办法只允许外部js文
我想在项目中使用带 noImplicitAny 和 strictNullChecks 的 typescript 声明一个带有错误优先回调的函数。 有没有一种方法可以声明一个interface 或typ
我通常设置我的 tsconfig.json 并将 strict 设置为 true。这意味着 noImplicitAny 也设置为 true。但是,当设置了 strict 时,typescript 似乎
简单的问题 - 每次我导入一个没有 .d.ts 文件的新 npm 库时,我都会创建一个 stub .d.ts 文件,其中包含很多这样的行,只是为了编译: function SomeFunctionIN
我正在使用 Typescript 2 和 Webpack 2 构建 Angular 2 应用程序。作为加载程序,我使用 awesome-typescript-loader。我在 tsconfig.js
Typescript 在 javascript es6 之上添加了许多功能,我对静态类型和private/public/protected 关键字很感兴趣。 到目前为止,Typescript 编译器施
我总是使用标志 --noImplicitAny 编译 TypeScript。这是有道理的,因为我希望我的类型检查尽可能严格。 我的问题是使用以下代码时出现错误: Index signature of
我总是使用标志 --noImplicitAny 编译 TypeScript。这是有道理的,因为我希望我的类型检查尽可能严格。 我的问题是使用以下代码时出现错误: Index signature of
我是一名优秀的程序员,十分优秀!