【问题标题】:TypeScript compiler reports issues at invalid placesTypeScript 编译器在无效位置报告问题
【发布时间】:2017-03-09 19:42:02
【问题描述】:

整个 main.ts:

import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';

// depending on the env mode, enable prod mode or add debugging modules
if (process.env.ENV === 'build') {
  enableProdMode();
}

export function main() {
  return platformBrowserDynamic().bootstrapModule(AppModule);
}

if (document.readyState === 'complete') {
  main();
} else {
  document.addEventListener('DOMContentLoaded', main);
}

一些错误(不是全部):

[default] /xxx/src/main.ts:11:96
    Index signature of object type implicitly has an 'any' type.
...
[default] /xxx/src/main.ts:11:853
    Parameter 'store' implicitly has an 'any' type.

报告的位置(例如 11:96、11:853)是无用的 - 那里什么都没有。我应该如何处理这些严重报告的错误?我怎样才能找到这些错误的真实位置?如何修复它,使其正确报告?

我正在使用这个种子 - https://github.com/preboot/angular2-webpack/(我认为主文件未修改)。


我在tsconfig.json添加了以下内容:

    "noImplicitAny": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "strictNullChecks": true,
    "forceConsistentCasingInFileNames": true,
    "skipLibCheck": true

请注意,我想要所有这些支票。

这也很奇怪,它报告的标识符不在我的代码中。例如:

Parameter 'dependencies' implicitly has an 'any' type.

由于这个和大量错误,我开始怀疑它在库中报告错误(这很奇怪,因为我有 skipLibCheck)或在连接的结果文件中(这也很奇怪,因为一切都在编译到 es5)。但是我没有使用 TypeScript 编译器或 Webpack 的经验,所以我真的说不出来。


Edit1:在 TypeScript repo 询问后,很明显编译器工作正常。罪魁祸首可能是 TS 加载器或 webpack 本身。

Edit2:已经一个多月了,webpack 伙计们doesn't seem to be doing anything 关于它:(。

【问题讨论】:

    标签: angular typescript webpack


    【解决方案1】:

    那是因为在您的tsconfig.json 中有一个名为 "noImplicitAny": true.

    你有两个选择:

    1. 要么将其设置为 false
    2. 在每个函数上声明返回类型(返回类型可能是any

    【讨论】:

    • 我在main.ts 中添加了返回该函数,但没有任何改变。虽然您对noImplicitAny 的看法可能是正确的,但我不想禁用它。我想要报告正确位置的正确错误报告。我会为这个问题提供更多信息,也许它会帮助缩小问题范围。
    猜你喜欢
    • 2020-06-16
    • 1970-01-01
    • 2015-05-21
    • 2012-09-23
    • 1970-01-01
    • 1970-01-01
    • 2019-12-08
    • 1970-01-01
    相关资源
    最近更新 更多