@types 模块未找到 Angular 2

@types module not found with Angular 2

我花在这上面的时间比我愿意承认的要多,所以我举手。

我有一个模块正试图包含在我的 angular 2 项目中:

import * as d3ScaleChromatic from 'd3-scale-chromatic';

这给出了一个错误:

Cannot find module 'd3-scale-chromatic'.

我已经通过 npm 安装 npm install --save @types/d3-scale-chromatic。我可以看出它住在 node_modules/@types/d3-scale-chromatic.

我的 tsconfig.json 看起来像这样:

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"   <-- should look here, right?
    ],
    "lib": [
      "es2016",
      "dom"
    ]
  }
}

所以我可以看到它会在 node_modules/@types.

中查找

它似乎与这个问题类似,但据我所知,我已经遵循了线程中的建议:https://github.com/Microsoft/TypeScript/issues/9725

任何人都可以告诉我在我的模块中正确引用这个包我缺少什么吗?

编辑:仔细检查后,我的应用程序中实际上有 4 个 tsconfig.* 文件。我怀疑这是问题的根源。现在的问题是,我需要保留哪些?

tsconfig.app.json :

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/app",
    "baseUrl": "./",
    "module": "es2015",
    "types": ["@types/d3-scale-chromatic"]
  },
  "exclude": [
    "test.ts",
    "**/*.spec.ts"
  ]
}

tsconfig.spec.json :

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/spec",
    "baseUrl": "./",
    "module": "commonjs",
    "target": "es5",
    "types": [
      "jasmine",
      "node"
    ]
  },
  "files": [
    "test.ts"
  ],
  "include": [
    "**/*.spec.ts",
    "**/*.d.ts"
  ]
}

tsconfig.e2e.json :

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/e2e",
    "baseUrl": "./",
    "module": "commonjs",
    "target": "es5",
    "types": [
      "jasmine",
      "jasminewd2",
      "node"
    ]
  }
}

您还需要安装:

npm install --save d3-scale-chromatic

连同:

npm install --save @types/d3-scale-chromatic