microsoft / dts-gen

dts-gen creates starter TypeScript definition files for any module or library.
MIT License
2.43k stars 102 forks source link

Could you add the ability to dts-gen individual files #58

Open csuarezgfx opened 7 years ago

csuarezgfx commented 7 years ago

with an option to specify an output directory and name?

Thanks

RyanCavanaugh commented 7 years ago

I'm not sure what this means. Can you sketch out what the behavior should be?

csuarezgfx commented 7 years ago

Sure, I'd like to generate typescript definition file for a single javascript file. So if I could dts-gen <output/directory/original.d.ts> Something like that. Thank you

RyanCavanaugh commented 7 years ago

Maybe you could give an example of an input JS file and an output .d.ts file from this process?

csuarezgfx commented 7 years ago

ok. I have a collada loader in my node_modules folder which consists of only one file. threejs-collada-loader I'd like to generate a .d.ts file for it. That is what dts-gen does correct?

SimoneTosato commented 7 years ago

+1 for this

venik commented 6 years ago

+1

nvivo commented 6 years ago

+1

nvivo commented 6 years ago

I got a workaround by doing the following:

  1. Load the page with the script you want to generate the definition on Firefox
  2. On console, call yourObj.toSource() (toSource is only available on Firefox)
  3. Save the string on a yourlib.js
  4. Call dts-gen --expression-file yourlib.js

@RyanCavanaugh What is needed here is a way to call dts-gen jquery.js -expr $ --out jquery.d.ts for example, and dts-gen would load jquery.js, look at the $ object and generate the jquery.d.ts out of that.

nikhilexe commented 6 years ago

+1, I have an UMD module generated through browserify. For some reason I had to include both enzyme and react in the UMD module. In my typescript I imported this UMD module, everything works fine except intellisense is not working. I guess .d.ts will help here and it would be helpful if tool can get declaration from the UMD module.

manofspirit commented 5 years ago

+1

DiogoVazTavares commented 4 years ago

+1

nolawnchairs commented 4 years ago

+1e6

aminya commented 4 years ago

👍 A lot of packages are not npm packages, so you can't install them as a module!

Running this gives: Couldn't load module "undefined". Please install it globally (npm install -g undefined) and try again.

dts-gen --expression-file yourlib.js
foo123 commented 4 years ago

I have a lib that is a module but not an npm package so I try to generate .d.ts with dts-gen via an expression:

dts-gen -e "require('lib.js')" -f lib.d.ts

but I get the error:

Couldn't load module "undefined". Please install it globally (npm install -g undefined) and try again.

I though that requiring the lib as an expression would work. So a way is needed to be able to gen dts files from a file that IS NOT an installed npm package.

So +1 for a solution to this

ststeiger commented 4 years ago

Yea, it's annyoing. I should generate the d.ts of leaflet-draw. First it complains about window and document not being defined. Great, because leaflet is not a nodejs module, and browser-only libraries are so rare, especially with JS.

Another great thing is, I now declare

var window = window || {};
var document = document || {};

on top of the file, and now it complains about Leaflet not being defined. Then I define it var L = L || {};

and now it complains about L.drawLocal.extend

Cannot read property "extend" of undefined.

Sure a "very" utilizable tool.

It's a good thing after all, that the files in DefinitelyTyped are not up-to-date, so TypeScript becomes an unproductivity tool. The rod between the wheels, so to say.

And cool, now I finally got it to work: npm install -g leaflet

edit leaflet and leaflet-draw to have this on the top, path of leatlet is as per error message in /root/.nvm/versions/node/v14.3.0/lib/node_modules/leaflet/dist/leaflet-src.js

const Window = require('window');
const window = new Window();
const navigator = require('navigator');

const { JSDOM } = require('jsdom');
const DEFAULT_HTML = '<html><body></body></html>';
const jsdom = new JSDOM(DEFAULT_HTML, {
    url: "https://www.bbc.co.uk",
    referrer: "https://www.bbc.co.uk",
    contentType: "text/html",
    userAgent: "node.js",
    includeNodeLocations: true
});
const document = jsdom.window.document;
const L= require('leaflet'); // in leaflet-draw only.

dts-gen --expression-file ./leaflet.draw-src.js

and we now have the "resounding" victory of a file "leaflet_draw_src.d.ts" generated, with content


/** Declaration file generated by dts-gen */

declare const leaflet_draw_src: any;

Ahahaha. It would be funny, if I wouldn't need it.

RafaelSundorf commented 3 years ago

3 Years now, is there any progress? Because there are still people like me who need that

felippi commented 3 years ago

Have some news to ignore required files?

aminya commented 3 years ago

I fixed the issue in #168. We should wait until someone merges it.

tomaszs commented 1 year ago

What's the progress here?

aminya commented 1 year ago

I have published my PR until it is merged:

npm install -g @aminya/dts-gen

dts-gen --expression-file=./your_file.js