简体   繁体   中英

Using Typescript 2 @Types with typescript 1.8.10

Currently i'm trying to use d3-drag v4 types in my project. were using Ts 1.8.10 and not ready to move to the TS2 beta.

the d3-v4 typings library is located here: https://github.com/tomwanzek/d3-v4-definitelytyped

i tried to install the typings using:

typings install d3-drag=github:tomwanzek/d3-v4-definitelytyped/src/d3-drag/index.d.ts#4d09073c046b6444859c66ff441f1e7691777d0f --save

but i'm getting the following error:

typings ERR! caused by /tomwanzek/d3-v4-definitelytyped/47eae6d/src/d3-selection.d.ts responded with 404, expected it to equal 200

i found somebody asking the same question here:

https://github.com/tomwanzek/d3-v4-definitelytyped/issues/93

but it doesn't answer my problem because i cant migrate to ts2 just yet. is there anyway using @types with TS 1.8.10?

meanwhile i created a d.ts file to be able to use drag. no intellisense and no type check, but at least i can use the drag library.

 declare var d3Drag;
 declare module 'd3-drag' 
{ 
export = d3Drag;
 }

I use it in my code like this:

import * as d3Drag from 'd3-drag';

...
let dragBehaivor = d3Drag.drag().on("start", dragStartFunction);

You can acquire the necessary files with

npm install @types/d3-selection --save

That will put the definition folder in node_modules/@types. You should then be able to copy the d3-selection folder to the typings folder and add the reference to the index.d.ts file there. I haven't tried it because I am moving to TS2, but it seems like it should work.

The short answer is that it is unfortunately not possible to use @types for D3 with TypeScript 1.8.x. For two reasons:

  1. The entire definitions/module resolution the TS2 compiler performs to use definitions installed from npm @types is not available in TS 1.8.x
  2. The definitions you are referring to https://github.com/tomwanzek/d3-v4-definitelytyped have now been migrated to the DefinitelyTyped/types-2.0 branch. They are the ones feeding @types for @types/d3 (Standard D3 v4 Bundle), @types/d3-selection , @types/d3-drag etc. All of them where written in a definition module structure only supported as of TS 2. They also use other TS features, like this typing of function contexts.

For these two reasons they will not be viable for TS 1.8.

Hopefully, you have been able to upgrade since you originally posted, since TS 2 is no longer beta.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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