pwnall / node-xhr2

XMLHttpRequest emulator for node.js
MIT License
104 stars 41 forks source link

Typings for use with Typescript? #45

Closed tonysamperi closed 1 year ago

tonysamperi commented 3 years ago

Hello,

is there any chance to have typings? I could lend a hand to achieve this

tonysamperi commented 3 years ago

I just saw there's a PR pending: #20

mathiskeeper commented 1 year ago

hi, Was wondering how to resolve the typings issue . I install the xhr2 in a typescript project. It install , but on the import im getting :

Could not find a declaration file for module 'xhr2'. '/Users/myoung/projects/qatest/node_modules/xhr2/lib/xhr2.js' implicitly has an 'any' type.

Try npm i --save-dev @types/xhr2 if it exists or add a new declaration (.d.ts) file containing declare module 'xhr2';

Thanks for any help in advance

tonysamperi commented 1 year ago

@mathiskeeper you create a typing file in your project and add it to your tsconfig! This is how I think I solved it...

mathiskeeper commented 1 year ago

@tonysamperi So i added a index.d.ts to the xhr2 node module ,then added it to the tsconfig json with tag. : "typeRoots": [ "./typings", "./node_modules/@types/" ] }

after that for TS files only. I added the import with global.XMLHttpRequest = require('xhr2').XMLHttpRequest and that allowed it to work

tonysamperi commented 1 year ago

@mathiskeeper well that's not exactly the clean way to do what I said, but if it works... Cheers!