在Visual Studio代码

时间:2016-12-15 16:26:21

标签: d3.js typescript

我对打字稿很新。我使用Visual Studio Code作为我的编辑器并使用gulp来执行typescript。我希望在我的打字稿代码中使用d3。我用下面的方法安装了d3,

npm install d3
typings install d3 --save

以下是我的package.json

  {
 "name": "proj",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "gulp": "^3.9.1",
    "gulp-typescript": "^3.1.3",
    "typescript": "^2.1.4"
  }
}

typings.json

{
  "dependencies": {
    "d3": "registry:npm/d3#3.0.0+20160723033700"
  }
}

tsconfig.json

{
    "files": [
        "src/main.ts",
        "src/greet.ts"
    ],
    "compilerOptions": {
        "noImplicitAny": true,
        "target": "es5"
    }
}

当我在TS代码中导入d3时,如下所示,

import * as d3 from "d3";

它给了我以下错误

[ts]无法找到模块“d3”的声明文件。 'c:/.../ VS / proj / node_modules / d3 / build / d3.node.js'含有'any'类型

任何人都可以让我知道我做错了什么

谢谢!

1 个答案:

答案 0 :(得分:0)

即使我面临同样的问题, 但后来我尝试在index.html文件中调用它,如

`<script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script>`

然后,在你的.ts文件中声明d3,如下所示,

declare var d3: any;

希望这能解决问题。