78

Say for example, you have a npm library, in my case mongoose, how would you go about generating d.ts files?

2

6 Answers 6

81

JavaScript doesn't always contain enough type information for the TypeScript compiler to infer the structures in your code - so automatically generating a definition based on JavaScript is rarely an option.

There are instructions on how to write them from scratch here:

https://www.stevefenton.co.uk/2013/01/complex-typescript-definitions-made-easy/

But there is one trick that might work (it only works in a limited set of cases).

If you paste the JavaScript into a new TypeScript file, fix any trivial errors you may get and compile it using the definition flag, it may be able to get you a file that would at least be a starting point.

tsc --declaration js.ts
Sign up to request clarification or add additional context in comments.

5 Comments

Steve Fenton's link is awesome. With one line you can get complex libraries working without needing to define all the methods. Check out option #2 in his post.
I dont think this is a valid answer to be accepted, Question is asked about generating, you cant always hand warping all the js library, they are HUGE. If hand warping is only option, then this question is no more being asked. So, take a look on "generating dts": github.com/SitePen/dts-generator
@nyconing - the line of code at the end of the answer will generate a type definition (i.e. not by hand). Additionally, since 2013, the awesome TypeScript team have added the ability to run the compiler against JavaScript files, so you don't even have to paste it into a TypeScript file to benefit from this trick.
@Fenton --declaration and --allowjs are exclusive, so you can't dump definitions from .js files, can you?
@KonstantinPelepelin in my answer I go the route of jamming the JavaScript into a .ts file in order to generate a starting point .d.ts file. So I'm not using allowJS for this.
38

The question is a bit old, but for the sake of people coming from search engines like me, if you are looking for an automated tool, check out:

  • Microsoft/dts-gen

    The official starting point Microsoft uses when creating types. It's meant to be a starting point only though and I didn't get a lot of luck depending just on it

  • dtsmake

    This one looks very promising. It depends on Ternjs which some editors use to provide autocomplete for JS code. Check Ternjs out also for other tool names and comparisons with them.

Update (2021): It's a good idea to check npmtrends and compare dts-generator, dts-gen, dtsmake, npm-dts, and alternatives.

2 Comments

Is it impossible to read JS file and make type file for TS in completely automated fashion ? I'm not experienced in generating type file from js
For folks coming to this answer in the year 2023, we now use the power of LLMs to do this kind of work 💪🤖 OpenAI 3.5 model can create pretty accurate types based on pasted context
20

For other people who find this post through google: there's a generator now by Microsoft itself: dts-gen.

1 Comment

9

If you're attempting to use a third-party JavaScript library in your TypeScript file, you can create a custom (and nearly blank) declaration file anywhere in your project.

For example, if you wanted to import:

import * as fooLibrary from 'foo-lib';

You would create a new file named 'foo-lib.d.ts' with the following contents:

declare module 'foo-lib' {
  var fooLibrary: any;
  export = fooLibrary;
}

Comments

5

For my particular situation, where I was working with obfuscated code from a third party, I found it useful to load the script in a page, and then use the console to log an instance of the obfuscated class. The console gives you a neat summary of the class methods and properties which you can copy and use as the starting point of a definition file.

> o = new ObfuscatedClass()
> console.log(o)
ObfuscatedClass
- methodA(a,b){some implementation}
- methodB(a,b){other implementation} etc

which you can copy paste and edit to

declare class ObfuscatedClass {

    methodA(a,b);
    methodB(a,b);

}

Comments

1

You might want to have a look at "npm-dts" NPM package. It is a tool for generating single index.d.ts file for your NPM package so that it could be consumed by other TypeScript packages out of the box.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.