简体   繁体   English

Typescript中的d3-tip库

[英]d3-tip library in Typescript

I am a newbie to typescript and was trying to include d3-tip library ( https://github.com/Caged/d3-tip ) to my project. 我是打字稿的新手,并尝试将d3-tip库( https://github.com/Caged/d3-tip )包含到我的项目中。 Normally, I would use 通常我会用

declare var [name_of_library]: any;

to let the compiler know the namespace, but d3-tip seems to be injected under the d3 namespace: 让编译器知道名称空间,但是d3-tip似乎注入了d3名称空间:

(function (root, factory) {
    if (typeof define === 'function' && define.amd) {
        // AMD. Register as an anonymous module with d3 as a dependency.
        define(['d3'], factory)
    } else {
        // Browser global.
        root.d3.tip = factory(root.d3)
    }
}(this, function (d3) {  
    ...

What is the correct way to use d3-tip in Typescript project? 在Typescript项目中使用d3-tip的正确方法是什么?

Based on https://github.com/borisyankov/DefinitelyTyped/blob/master/d3/d3.d.ts you have 基于https://github.com/borisyankov/DefinitelyTyped/blob/master/d3/d3.d.ts

declare var d3: D3.Base;

This means to add to d3 you simply add to D3.Base so: 这意味着要添加到d3 ,只需将其添加到D3.Base

declare module D3 {
    export interface Base{
        tip: any;
    }
}

With the current structure of the d3.d.ts file ( https://github.com/borisyankov/DefinitelyTyped/blob/master/d3/d3.d.ts ), this is how you'd accomplish it now: 使用d3.d.ts文件的当前结构( https://github.com/borisyankov/DefinitelyTyped/blob/master/d3/d3.d.ts ),这是您现在要完成的方式:

declare module d3 {
    export var tip: any;
}

The "correct" solution, however, is to write a full type definition file. 但是,“正确”的解决方案是编写完整的类型定义文件。 The above is simply a quick way to silence the compiler. 上面只是使编译器静音的一种快速方法。

For me such thing works with angular 4 (typescript 2.2, @types/d3:'^3.5.5' @types/d3-tip: '^3.5.5' "d3": "^3.5.5","d3-tip": "^0.7.1",)... 对我来说,这样的东西适用于angular 4(打字稿2.2,@ types / d3:'^ 3.5.5'@ types / d3-tip:'^ 3.5.5'“ d3”:“ ^ 3.5.5”,“ d3- tip“:” ^ 0.7.1“,)...

declare var d3:any;
import 'd3';
import * as d3tip from 'd3-tip';
d3.tip = d3tip;

I agree it is a bit strange.., but however it works. 我同意这有点奇怪..但是可以。

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

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