【问题标题】:Error using D3 with typescript in Visual Studio Code在 Visual Studio Code 中使用带有 typescript 的 D3 时出错
【发布时间】:2016-12-15 16:26:21
【问题描述】:

我对打字稿很陌生。我使用 Visual Studio Code 作为我的编辑器并使用 gulp 来执行打字稿。我希望在我的打字稿代码中使用 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' 类型

谁能告诉我我做错了什么

谢谢!

【问题讨论】:

    标签: d3.js typescript


    【解决方案1】:

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

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

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

    declare var d3: any;
    

    希望这能解决问题。

    【讨论】:

      【解决方案2】:

      安装这个依赖:

      npm i --save-dev @types/d3
      

      如果您使用 VS Code 作为编辑器,在安装该依赖项后,重新加载窗口以使错误也从 VS Code 中消失。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-03-18
        • 2017-05-25
        • 1970-01-01
        • 2018-02-03
        • 2017-03-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多