1

似乎这个问题与 IDE WebStorm 有关。我向 WebStorm 报告。并在这里追踪。

我正在使用 Angular 2 和 TypeScript 2。

如何显式使用lib.d.ts类型的位置?因为现在它在我的 IDE WebStorm 2016.3 EAP 中显示为红色:

const hostname = location.hostname;
const hostname = window.location.hostname;

在此处输入图像描述


我的文件中有这个:

import { Location } from '@angular/common';

constructor(private _location: Location) {}
// note there is an underline before
// and when I use in other functions, I actually use 'this._location' not just '_location'

我发现删除后import { Location } from '@angular/common';,错误就会消失。


我猜这个错误显示的原因是因为 IDE 认为这是location来自 Angular 2。

您可以从下面的屏幕截图中看到属于LocationAngular 2 的所有功能。

在此处输入图像描述


一种方法是使用const hostname = (location as any).hostname;,但有更好的方法吗?谢谢

我的tsconfig.json如果有帮助:

{
  "compilerOptions": {
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "module": "commonjs",
    "removeComments": true,
    "sourceMap": true,
    "lib": ["es6", "dom"]
  },
  "include": [
    "node_modules/@types/**/*.d.ts",
    "src/**/*.ts"
  ],
  "exclude": [
    "node_modules",
    "!node_modules/@types/**/*.d.ts"
  ],
  "compileOnSave": false,
  "buildOnSave": false,
  "atom": {
    "rewriteTsconfig": false
  }
}
4

1 回答 1

1

感谢@NitzanTomer 的帮助。

这个问题似乎与 WebStorm 有关。

我将其报告给 WebStorm,您可以在此处跟踪问题:

https://youtrack.jetbrains.com/issue/WEB-23021

于 2016-08-29T22:39:27.307 回答