将 Nodejs 与 Typescript 结合使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14083427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:47:20  来源:igfitidea点击:

Using Nodejs with Typescript

javascriptnode.jswebwebservertypescript

提问by lhk

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

我想在我的打字稿项目中使用 Nodejs 并从https://github.com/borisyankov/DefinitelyTyped/tree/master/node下载 node.d.ts

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

但是VisualStudio在.d.ts文件中发现了很多错误。它们基本上都是一样的:

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

错误 2 导出接口 '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

我搜索了SO并发现了这个问题: 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.

其中一条评论给出了从 Microsoft 示例下载 node.d.ts 的建议。我浏览了互联网,找到了这篇文章http://stackful.io/blog/typescript-nodejs-vim-and-linux-oh-my/并下载了 node.d.ts 的第二个版本,结果完全相同。

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

我在哪里可以找到适合 nodejs 和 Typescript 的定义文件?

采纳答案by JohnnyHK

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

对于 0.8.1-1 版本的 TypeScript,使用node.d.ts来自这个 github 项目的最新文件:https: //github.com/soywiz/typescript-node-definitions

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

在我exported interface从那里提取最新的定义文件之前,我遇到了同样的错误。

回答by Garrett Serack

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.

我怀疑 Typescript 的 (0.8.1~1) node.js 包的当前版本破坏了一些东西。我也遇到过这个问题。我调整了 node.d.ts 文件并标记了一些“导出”而不是“声明”,这让我解决了编译问题,但我发现这些函数没有出现在当前范围内。

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.

FWIW,我一直在玩 typescript-require 和 typescript-wrapper 的个人分支(这两个都需要一些调整才能使用 0.8.1~1)而且我不太确定一切是否正常应该是这样,所以我不确定问题是我的问题还是当前节点/打字稿的特有问题。