简体   繁体   English

使用Typescript时在外部库中使用公共变量和接口

[英]Using public variables and interfaces in external libraries when using Typescript

I am fairly new to Typescript and am developing an Angular 2.0/Typescript application. 我是Typescript的新手,正在开发Angular 2.0 / Typescript应用程序。 I have made d3 available as a typing and I would like to use public methods from D3 as well as interfaces. 我已经使d3可以用作输入,并且我想使用D3的公共方法以及接口。

To make d3 available I have added the following code: 为了使d3可用,我添加了以下代码:

declare var d3

I need to use the following interface: 我需要使用以下界面:

private _axises: D3.Selection

and the following public method. 以及以下公共方法。

this._axises = d3.select(...)

How do I make both "d3" and "D3" available for usage without the Typescript transpiler throwing any errors? 如何在Typescript编译器不会引发任何错误的情况下使“ d3”和“ D3”同时可用?

Thanks 谢谢

How do I make both "d3" and "D3" available for usage without the Typescript transpiler throwing any errors? 如何在Typescript编译器不会引发任何错误的情况下使“ d3”和“ D3”同时可用?

You seem to want to use community written d3 defintions. 您似乎想使用社区编写的d3定义。 In that case you should not add your own ie don't have declare var d3 anywhere in your code. 在这种情况下,你不应该添加自己的 ,即没有declare var d3在你的代码的任何地方。 This should come with d3.d.ts available here : https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/d3 这应该带有d3.d.ts可从此处获得: https : //github.com/DefinitelyTyped/DefinitelyTyped/tree/master/d3

More 更多

You can install it using typings typings install d3 --ambient --save . 您可以使用typings install d3 --ambient --save来安装它类型typings install d3 --ambient --save Also if you are using a module system use import * as d3 from "d3" eg in alm.tools I have https://github.com/alm-tools/alm/blob/894a6f095ecc84c8e32adf79c1bb7d595eba877d/src/app/tabs/dependencyView.tsx#L8 另外,如果您正在使用模块系统,请使用import * as d3 from "d3"中的import * as d3 from "d3"例如在alm.tools中,我有https://github.com/alm-tools/alm/blob/894a6f095ecc84c8e32adf79c1bb7d595eba877d/src/app/tabs/dependencyView。 TSX#L8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM