简体   繁体   English

TypeScript和d3域类型

[英]TypeScript and d3 domain type

I have a problem, when I want to use domain. 我有一个问题,当我想使用域名时。

So, I try to use next part of my code 所以,我尝试使用我的代码的下一部分

var x = d3.time.scale()
  .domain([new Date('2016-06-10'), new Date('2016-06-25')])
  .rangeRound([0, 1000]);

And I get error about error types for domain... 我收到有关域的错误类型的错误...

Argument of type 'Scale' is not assignable to parameter of type 'Scale'. “Scale”类型的参数不能分配给“Scale”类型的参数。

Types of property 'domain' are incompatible. 属性“域”的类型不兼容。

Type '{ (): Date[]; 输入'{():Date []; (dates: number[]): Scale; (日期:数字[]):比例; (dates: Date[]): Scale; (日期:日期[]):比例; }' is not assignable to type '{ (): number[]; }'不能赋值给'{():number []; (values: number[]): Scale; (值:数字[]):比例; }'. }”。

Type 'Date[]' is not assignable to type 'number[]'. 类型'日期[]'不能分配给'number []'。 Type 'Date' is not assignable to type 'number'. “日期”类型不能指定为“数字”类型。

But If we look d3 typings 但是,如果我们看看d3打字

domain(): Date[];
domain(dates: number[]): Scale<Range, Output>;
domain(dates: Date[]): Scale<Range, Output>;

domain can take Date array! 域名可以采取日期数组!

If we disable types, then all works fine. 如果我们禁用类型,那么一切正常。

And I have similar problem with another part of my code: 我的代码的另一部分有类似的问题:

var drag = d3.behavior.drag()
  .on('drag', function (d) {
    d3.event.sourceEvent.stopPropagation();
  })
  .on('dragstart', function () {
    //
  });

I get error about: 我得到错误:

Property 'sourceEvent' does not exist on type 'Event | 类型'Event |上不存在属性'sourceEvent' BaseEvent'. BaseEvent”。

But If we look in types 但是如果我们看一下类型

export var event: Event | BaseEvent;

and

interface BaseEvent {
     type: string;
     sourceEvent?: Event;
 }

I use typings: 我用的是打字:

"d3": "registry:dt/d3#0.0.0+20160614091820" “d3”:“注册表:dt / d3#0.0.0 + 20160614091820”

and awesome-typescript-loader for webpack 和webpack的awesome-typescript-loader

So, what's wrong with me? 那么,我怎么了?

Is your intent to use D3 v4 or D3 v3? 您是否打算使用D3 v4或D3 v3?

The code snippets you posted seem to imply D3 v3. 你发布的代码片段似乎意味着D3 v3。 In line with what Jake said. 与杰克所说的一致。

There are current D3 version 4 definitions available, assuming you are using TypeScript 2. 假设您使用的是TypeScript 2,则可以使用当前的D3版本4定义。

The repo he referenced has been completely migrated to DefintitelyTyped (currently types-2.0 branch), we had mostly used it for collaborative development, while the ecosystem was still in flux... 他引用的repo已经完全迁移到DefintitelyTyped (当前类型为2.0分支),我们主要使用它进行协作开发,而生态系统仍在不断变化......

Now to get the definitions by module simply use npm install @types/d3-selection --save (or --save-dev if you do not publish for third party use) 现在按模块获取定义只需使用npm install @types/d3-selection --save (或--save-dev,如果你不发布第三方使用)

As of yesterday, npm install @types/d3 --save will also get you definitions corresponding to the Standard D3 Bundle. 截至昨天, npm install @types/d3 --save还将为您提供与标准D3套件相对应的定义。

There was some technical debt in the publication process from DefinitelyTyped to npm @types as we did not want to break libraries that still needed the legacy D3 v3.5 definitions. 从DefinitelyTyped到npm @types的发布过程中存在一些技术债务,因为我们不想破坏仍需要传统D3 v3.5定义的库。 This is now resolved. 现在已经解决了。

Once you install the relevant definitions, you simple use TypeScript/ES6 import statements for the corresponding D3 modules (or the d3 standard bundle). 安装相关定义后,只需对相应的D3模块(或d3标准软件包)使用TypeScript / ES6 import语句。

import { select, Selection } from 'd3-selection';
import * as d3Drag from 'd3-drag';

Or similar. 或类似的。 You can also create your own TypeScript module to bundle as subset of the modules, if it makes it more convenient for you see my answer to https://stackoverflow.com/questions/40314024/typescript-d3-v4-import-not-working 您还可以创建自己的TypeScript模块作为模块的子集进行捆绑,如果它更方便您查看我对https://stackoverflow.com/questions/40314024/typescript-d3-v4-import-not-的回答工作的

Things are slightly different, if you want to use the definitions in a vanilla script with a global 如果你想在带有全局的vanilla脚本中使用定义,情况就会略有不同

If that is your use case let me know, and I can add more info. 如果这是你的用例让我知道,我可以添加更多信息。

This happened to me yesterday using Typescript 2.0 and the latest @types/d3-drag . 我昨天使用Typescript 2.0和最新的@types/d3-drag发生了这种情况。

I think its just an issue with the typings, check here for updates: https://github.com/tomwanzek/d3-v4-definitelytyped/issues 我认为这只是一个问题,请点击此处查看更新: https//github.com/tomwanzek/d3-v4-definitelytyped/issues

My crud band-aid: 我的创可贴:

let drag:any = d3.behavior.drag()...

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

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