Say for example, you have a npm library, in my case mongoose, how would you go about generating d.ts files?
-
3Possible duplicate of How do you produce a .d.ts "typings" definition file from an existing JavaScript library?– Pierre ArlaudJul 26, 2016 at 13:10
-
For posterity: npmtrends comparing some of the libraries mentioned in this thread– m_floerSep 8, 2021 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
-
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. Oct 1, 2014 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": github.com/SitePen/dts-generator– nyconingSep 7, 2018 at 4: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.– FentonSep 21, 2018 at 16:31
-
@Fenton
--declarationand--allowjsare exclusive, so you can't dump definitions from.jsfiles, can you? Dec 7, 2018 at 14:08 -
3@KonstantinPelepelin in my answer I go the route of jamming the JavaScript into a
.tsfile in order to generate a starting point.d.tsfile. So I'm not usingallowJSfor this.– FentonDec 7, 2018 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– canbaxJun 28, 2019 at 13:07
For other people who find this post through google: there's a generator now by Microsoft itself: dts-gen.
-
7
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;
}
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);
}
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.