2

I am trying to create d.ts file from .js file. Many posts such as this are 2 years old How do you produce a .d.ts "typings" definition file from an existing JavaScript library?

My questions that after 2 years is there any easier way to extract type into from .js file into .d.ts file?

After running

npm run typings -- install jointjs --save

the generated .d.ts file got many definitions missing. That's why I would like to get a joint.js file that contains all the stuff as input.

Community
  • 1
  • 1
Shawn
  • 5,130
  • 13
  • 66
  • 109
  • Possible duplicate of [Is there a tool to convert JavaScript files to TypeScript](http://stackoverflow.com/questions/14412164/is-there-a-tool-to-convert-javascript-files-to-typescript) – choz Jul 29 '16 at 01:27
  • 1
    I am looking for tools to create .d.ts file, not tools to convert js into ts. – Shawn Jul 29 '16 at 01:34
  • [DefinitelyTyped](https://github.com/DefinitelyTyped/DefinitelyTyped) is a very good resource. I've yet to see even a moderately-popular library that doesn't have a corresponding `.d.ts`. It already has [jointjs.d.ts](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/jointjs/jointjs.d.ts). – Frank Tan Jul 29 '16 at 03:01
  • 1
    Possible duplicate of [How do you produce a .d.ts "typings" definition file from an existing JavaScript library?](http://stackoverflow.com/questions/12687779/how-do-you-produce-a-d-ts-typings-definition-file-from-an-existing-javascript) – Frank Tan Jul 29 '16 at 03:03
  • I have got jointjs.d.ts from DefinitelyTyped. But it doesn't include the ERD portion which I am looking for. Also I referenced **How do you produce a .d.ts "typings" definition file from an existing JavaScript library?** and hope there would be some new tools coming up after several years. – Shawn Jul 29 '16 at 14:33
  • You cannot "generate" a typings definition from a JS file. By definition, JS doesn't include types, so any automatic conversion would fail miserably. In this specific case what people do is manually edit the definition module to declare what kind of API it has. If the existing @types/jointjs definition is missing a portion, you need to manually write the actual definitions for it with a best guess for what each interface needs or returns. That's how those types have been maintained. – zeh Sep 11 '18 at 22:43

1 Answers1

0

This solution works pretty well without the need for installing any extra dependencies if you already have "typescript" (3.7 or above) installed: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#getting-the-dts-from-a-javascript-file

Maneet
  • 61
  • 1
  • 9