Definition (.d.ts) generator

Topics: General
Oct 21, 2012 at 6:58 PM

Please create a tool to generate .d.ts file from current JavaScript files and libraries.


Thanks

Oct 21, 2012 at 8:39 PM
Edited Oct 21, 2012 at 8:41 PM

This is not a trivial task, there is not much benefit of having it 1 level deep besides autocompletion.

To really benefit you need all the subobjects and how they are structured, or inteded to. You theoretically use an example app to generate some of then, but who ensures that this is bug free? This is a really difficult problen to solve, you need a sophisticated generator for that.

The only one who can really make a great declaration file are humans, they know how all the objects are intended to work together.
This is a lot of work, so i guess this will only done for the most popular ones. I started with underscore and backbone, but don't see much benefit for going more than 1 level deep right now, i need the time for my work.

I extend them while using them, but this doesn't get shared with anyone and often i don't care to be honest. It worked in plain, why should i put all the effort in just for me. What we need is a system to collaborate step by step on a public repository for declaration files.

TypeScript wih VisualStudio is a big help for all the stuff on top of the library, so even without declaration files i would recommend to use it.

Oct 22, 2012 at 4:22 AM

A definitions file generator will be as good as the built-in IntelliSense which already uses some type-inference.

The best .d.ts files can be created only by people. You can find definitions here:

https://github.com/borisyankov/DefinitelyTyped

Anyone who has put effort in creating definitions not yet present there is welcome to contribute.

Oct 22, 2012 at 7:01 PM

I think investigating in custom jsdoc template could be a good start : D

Oct 25, 2012 at 2:18 AM

I suppose if someone used a very strict JSDoc format, then in theory, you might be able to make something to create definitions from the JSDoc work. This would probably be ideal for keeping definition files in sync with code. Granted the generator could only produce definition files as good as the information contained in the JSDoc comments, but that is the deal with all of this. Just my two cents.

Mar 20, 2013 at 3:06 PM
This actually looks like it could be quite straightforward using something like the Doctrine library: https://github.com/Constellation/doctrine
I'm surprised nobody has tried this already to accelerate the generation of declration files.
Jun 6, 2014 at 5:16 AM
I wrote a tool to do this. It's designed to handle the easy cases correctly, and to produce a useful starting point for manually fixing the difficult cases. It's written in TypeScript and uses esprima and doctrine.

It works best on closure-style modules; it can process the entire google closure library without error, though some ambiguities have to be resolved manually.

You can use it online at:
http://fivetran.github.io/closure-definitions/

Source repo:
https://github.com/fivetran/closure-definitions
Jun 6, 2014 at 1:14 PM
@georgewfraser - that's great!

I just tried a sample from the PhaserJS library and noticed that the annotations use
@param x {number} comment
rather than
@param {number} x comment
This tool could also help convert existing annotated JavaScript to TypeScript.
Jun 6, 2014 at 4:14 PM
Your second example is the standard JSDoc form. Unfortunately some people basically invent their own JSDoc, I'm dependent on doctrine for JSDoc parsing and it supports only the standard forms.
Jan 21, 2015 at 8:10 PM
Edited Jan 21, 2015 at 8:11 PM
@georgewfraser your tool looks great.
Has to change the .gitmodules file to have something like:
[submodule "lib/closure-library"]
    path = lib/closure-library
    url = https://github.com/google/closure-library.git
[submodule "lib/doctrine"]
    path = lib/doctrine
    url = https://github.com/fivetran/doctrine.git
[submodule "lib/definitely-typed"]
    path = lib/definitely-typed
    url = https://github.com/fivetran/DefinitelyTyped.git
[submodule "typescript"]
    path = typescript
    url = https://github.com/Microsoft/TypeScript.git
it would fail to "build" using the original file.
fetching the submodules now, let's see... fingers crossed.
Again, awesome tool!
Jan 21, 2015 at 9:42 PM
It worked fine in Linux, but not in Windows.
C:\Projects\github\typescript-closure-tools>tsc --module commonjs definition-generator/src/*.ts
error TS5007: Cannot resolve referenced file: 'definition-generator/src/*.ts'.
Jan 28, 2015 at 8:55 PM
closure-library was missing thanks. With respect to git@ vs https://, you have to set up github ssh to pull the submodules.