Skip to content
This repository has been archived by the owner on Dec 15, 2023. It is now read-only.

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

Open
csuarezgfx opened this issue May 6, 2017 · 20 comments · May be fixed by #168
Open

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

csuarezgfx opened this issue May 6, 2017 · 20 comments · May be fixed by #168

Comments

@csuarezgfx
Copy link

with an option to specify an output directory and name?

Thanks

@RyanCavanaugh
Copy link
Member

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

@csuarezgfx
Copy link
Author

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

@RyanCavanaugh
Copy link
Member

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

@csuarezgfx
Copy link
Author

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
Copy link

+1 for this

@venik
Copy link

venik commented Sep 11, 2017

+1

1 similar comment
@nvivo
Copy link

nvivo commented Sep 14, 2017

+1

@nvivo
Copy link

nvivo commented Sep 14, 2017

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
Copy link

+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
Copy link

+1

1 similar comment
@DiogoVazTavares
Copy link

+1

@nolawnchairs
Copy link

+1e6

@aminya
Copy link

aminya commented Apr 12, 2020

👍 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
Copy link

foo123 commented May 12, 2020

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
Copy link

ststeiger commented May 20, 2020

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
Copy link

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

@felippi
Copy link

felippi commented Apr 21, 2021

Have some news to ignore required files?

@aminya aminya linked a pull request Apr 22, 2021 that will close this issue
@aminya
Copy link

aminya commented Apr 22, 2021

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

@tomaszs
Copy link

tomaszs commented Jul 3, 2023

What's the progress here?

@aminya
Copy link

aminya commented Jul 3, 2023

I have published my PR until it is merged:

npm install -g @aminya/dts-gen

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.