我是 Typescript 的新手,正在开发 Angular 2.0/Typescript 应用程序。我已将 d3 作为类型提供,我想使用 D3 的公共(public)方法和接口(interface)。
为了使 d3 可用,我添加了以下代码:
声明 var d3
我需要使用以下接口(interface):
私有(private)轴:D3.Selection
和以下公共(public)方法。
this._axises = d3.select(...)
如何在 Typescript 转译器不抛出任何错误的情况下使“d3”和“D3”都可用?
谢谢
最佳答案
How do I make both "d3" and "D3" available for usage without the Typescript transpiler throwing any errors?
您似乎想使用社区 编写的 d3 定义。在那种情况下,您不应该添加您的,即您的代码中的任何地方都没有declare var d3
。这应该与 d3.d.ts
一起提供:https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/d3
更多
您可以使用 typings typings install d3 --ambient --save
安装它。此外,如果您使用的是模块系统,请使用 import * as d3 from "d3"
例如在 alm.tools我有 https://github.com/alm-tools/alm/blob/894a6f095ecc84c8e32adf79c1bb7d595eba877d/src/app/tabs/dependencyView.tsx#L8
关于javascript - 使用 Typescript 时在外部库中使用公共(public)变量和接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36001159/