在webpack升级后找不到命名空间NodeJS

Fel*_*lix 12 typescript webpack angular

我有使用WebPack构建的Angular2应用程序.我将WebPack从v1.8升级到v2,一切似乎都运行良好.唯一的问题是旧代码具有以下内容:

import Timer = NodeJS.Timer;
....
apptInterval: Timer;
....
this.apptInterval = setInterval(() => { ... }, 1000);
Run Code Online (Sandbox Code Playgroud)

升级后,这给了我一个错误: TS2503: Cannot find namespace 'NodeJS'.

tsconfig.json看起来像这样:

{
"compilerOptions": {
   "target": "es5",
   "module": "commonjs",
   "moduleResolution": "node",
   "sourceMap": true,
   "emitDecoratorMetadata": true,
   "experimentalDecorators": true,
   "lib": ["es2015", "dom"],
   "noImplicitAny": true,
   "suppressImplicitAnyIndexErrors": true
   }
}
Run Code Online (Sandbox Code Playgroud)

Angular/Webpack的文档不再具有typings.json; 但是,即使我从Webpack 1目录中复制,它也无济于事.内容是typings.json是

{
"globalDependencies": {
  "jasmine": "registry:dt/jasmine#2.2.0+20160621224255",
  "node": "registry:dt/node"
  }
}
Run Code Online (Sandbox Code Playgroud)

有趣的是,如果我删除对NodeJS的引用,一切正常.像这样:

apptInterval: any;
....
this.apptInterval = setInterval(() => { ... }, 1000);
Run Code Online (Sandbox Code Playgroud)

如果我查看F12调试器,apptInterval的类型是ZoneTask.我确信有一种方法可以使它变得强大,但它逃脱了我.我发现的唯一建议是运行typings install dt~node --global --save-dev(基本上更新typings.json,但没有帮助.

小智 34

如果您的工作目录中也有tsconfig.app.json,请尝试将属性节点添加到types字段.喜欢:

  {
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/app",
    "module": "es6",
    "baseUrl": "",
    "types": ["node"] --> ADD THIS
  },
  "exclude": [
    "test.ts",
    "**/*.spec.ts"
  ]
}
Run Code Online (Sandbox Code Playgroud)

  • 您还可以将 `types: ["node"]` 添加到 **tsconfig.json**,但请确保 **tsconfig.app.json** 中没有空的 `types: []` 定义,因为覆盖它(通过艰难的方式了解到......) (6认同)

uni*_*nal 5

对于TypeScript@2.0+,请使用@types

npm install -D @types/node @types/jasmine
Run Code Online (Sandbox Code Playgroud)

如果您仍然想继续使用typings,请添加typings/index.d.ts到tsconfig.json中:

{
  "include": [   // or "files"
    "typings/index.d.ts"
  ]
}
Run Code Online (Sandbox Code Playgroud)