Using Nodejs with Typescript

19,975

Solution 1

For the 0.8.1-1 version of TypeScript, use the latest node.d.ts file from this github project: https://github.com/soywiz/typescript-node-definitions

I was getting the same exported interface errors until I pulled the latest definition files from there.

Solution 2

I'm not entirely sure what the heck is going on here.

I suspect that the current version of the (0.8.1~1) node.js package for Typescript broke some stuff. I've been running into this too. I tweaked the node.d.ts file and marked a couple things "export" instead of "declare" and that got me past my compilation issues, but I'm finding that the functions are not showing up in the current scope.

FWIW, I have been playing around with a personal fork of typescript-require and typescript-wrapper (both of which required some tweaking to work with 0.8.1~1) and I'm wasn't too certain that everything is working as well as it should be, so I wasn't sure that the issue is my issue or something endemic to the current node/typescript.

Share:
19,975
lhk
Author by

lhk

Updated on June 04, 2022

Comments

  • lhk
    lhk almost 2 years

    I would like to use Nodejs in my typescript project and downloaded node.d.ts from https://github.com/borisyankov/DefinitelyTyped/tree/master/node

    But VisualStudio finds a lot of errors in the .d.ts file. They're all basically the same:

    Error 2 exported interface 'Server' extends interface from private module 'events' C:\Users\lhk\Desktop\typescript-game\strategy go\node\node-0.8.d.ts 236 37 node-0.8.d.ts

    All the error messages complain that a private interface is extended.

    I searched SO and found this question: nodejs require inside TypeScript file

    One of the comments gives the advice to download node.d.ts from the Microsoft samples. I browsed the internet, found this article http://stackful.io/blog/typescript-nodejs-vim-and-linux-oh-my/ and downloaded the second version of node.d.ts , with the exact same result.

    Where do I find a proper definitions file for nodejs and Typescript ?

  • lhk
    lhk over 11 years
    great, this worked. I compared the two .d.ts files and found a curious difference. In the modules section, whenever another module is imported, the old version uses "import something=module("something")", but the new version changes this to "export import something=module("something")". Why is it all of a sudden legit to extend an interface of a private module, if that module is exported ? The whole construct does not make sense to me. Why does for example the module http "export import" the module events ? Am I now able to do something like http.events.SomethingfromEvents ?
  • lhk
    lhk over 11 years
    The github project is fantastic, it solved another of my questions. If you would copy this answer to stackoverflow.com/questions/14084406/typescript-and-socket-i‌​o I would be glad to accept it
  • JohnnyHK
    JohnnyHK over 11 years
    @lhk I haven't looked into the details as to why it works, I was just glad it did. I looked at your other question, but that question indicates you're already using the socket.io.d.ts file from the soywiz project so I'm confused as to why you want me to copy this answer there.
  • lhk
    lhk over 11 years
    You're right, I mixed something up. I realized that this error occured in the socket.io.d.ts file, too, after I had posted this question. Then I switched to the new node.d.ts version and the error vanished in socket.io.d.ts. Since I had already solved the problem for the socket.io client, the error here was the only remaining one with socket.io. I'm sorry, I will now post my solution to the socket.io problem