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

- 80,178
- 33
- 141
- 199
-
3Possible 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) – Pierre Arlaud Jul 26 '16 at 13:10
-
For posterity: [npmtrends comparing some of the libraries mentioned in this thread](https://www.npmtrends.com/dts-gen-vs-dts-generator-vs-dtsmake-vs-quicktype-vs-npm-dts) – floer32 Sep 08 '21 at 23:37
6 Answers
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

- 241,084
- 71
- 387
- 401
-
4Steve 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. – John Tabernik Oct 01 '14 at 13:47
-
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": https://github.com/SitePen/dts-generator – nyconing Sep 07 '18 at 04:18
-
@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 Sep 21 '18 at 16:31
-
@Fenton `--declaration` and `--allowjs` are exclusive, so you can't dump definitions from `.js` files, can you? – Konstantin Pelepelin Dec 07 '18 at 14:08
-
3@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. – Fenton Dec 07 '18 at 14:49
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:
-
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
-
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.
-
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 – canbax Jun 28 '19 at 13:07
-
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](https://chat.openai.com/share/caaf7f2e-e309-4a89-bd0c-83b470b7dc01) – GFargo Jun 27 '23 at 13:29
For other people who find this post through google: there's a generator now by Microsoft itself: dts-gen.

- 640
- 6
- 8
-
8Note that [dts-gen doesn't support ES6 import/exports](https://github.com/Microsoft/dts-gen/issues/42). :-/ – Dan Dascalescu Jun 11 '17 at 19:09
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;
}

- 2,911
- 1
- 23
- 15
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);
}

- 1,887
- 2
- 19
- 27
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.

- 11
- 1