简体   繁体   English

'd3和'd3-hexbin'在typescript中作为全局库

[英]'d3 and 'd3-hexbin' in typescript as global libraries

So, I'm using d3 and d3-hexbin as global libraries: 所以,我使用d3d3-hexbin作为全局库:

<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/d3-hexbin.v0.2.min.js"></script>

... and referencing them in .ts as: ...并在.ts引用它们:

/// <reference types="d3" />
/// <reference types="d3-hexbin" />

... using DefinitelyTyped definitions. ...使用DefinitelyTyped定义。 However, although this works: 但是,虽然这有效:

const svg = d3.select('#hitmap').append('svg')

... this: ... 这个:

const hexbin = d3.hexbin().radius(binsize + 1)

... fails with a : ...失败了:

Property 'hexbin' does not exist on type 
  'typeof "/Users/bytter/node_modules/@types/d3/index"'

Thoughts? 思考?

Even though you have typings for d3, you don't have the derived typings for d3-hexbin. 即使你有d3的打字,你也没有d3-hexbin的派生类型。 So you've to fall back to the declare method like I did here for d3-cloud: Typings for d3-cloud 所以你要回到declare方法,就像我在这里为d3-cloud所做的那样:d3-cloud的打字

Basically, the steps you've to follow are these: 基本上,您要遵循的步骤是:

  1. import the d3 library like usual, but give it an alias: import * as D3 from 'd3'; 像往常一样import d3库,但给它一个别名: import * as D3 from 'd3'; (Notice: Capital D for D3) (注意:D3的资本D)

  2. declare d3 again so you can use it for hexbin: declare let d3: any; 再次declare d3,这样你就可以将它用于hexbin: declare let d3: any;

  3. Use D3 for everything concerning the parent d3 library and d3 for hexbin generation alone. D3用于父d3库的所有内容,将d3用于单独的hexbin生成。

const svg = D3.select('#hitmap').append('svg');

const hexbin = d3.hexbin().radius(binsize + 1);

This will prevent the editor from showing hexbin specific suggestions and Typescript will not be able to catch hexbin specific type errors. 这将阻止编辑器显示hexbin特定的建议,而Typescript将无法捕获hexbin特定类型的错误。 But unfortunately, till official typings arrive for hexbin, this is the best way I've found. 但不幸的是,在正式打包到达hexbin之前,这是我发现的最佳方式。

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

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