如何显式使用 lib.d.ts 类型?

How to explicitly use lib.d.ts types?

Seems like this issue is related with IDE WebStorm. I reported to WebStorm. And track here.

我正在使用 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 认为这是来自 Angular 的 location 2.

从下面的截图可以看出,Angular2.

属于Location的所有函数


一种方法是使用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
  }
}

感谢@NitzanTomer 的帮助。

看来这个问题与 WebStorm 有关。

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

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