使用 Node.js 和 TypeScript 的正确方法是什么?

What's a correct way to work with Node.js and TypeScript?

我刚开始将 Node.JS 与 TypeScript 一起使用,我遇到的第一件事是第一行的编译错误(使用 WebStorm):

import http = require('http');

我尝试使用 "var"、"module" 而不是 "require",但我仍然得到 "cannot find module http" 或 "cannot understand 'require' name"。一段时间后,我只能找到一个解决方案(@basarat) at Whosebug, which suggested to download and add to project some node.d.ts file,然后将其用作顶部的参考:

/// <reference path="node.d.ts" />

import http = require('http');

但是,它看起来不太好,因为我希望 Microsoft 的产品或多或少可以开箱即用,而无需使用第 3 方 hack。此外,看起来我需要将此参考文件添加到我需要使用 node.js 模块的每个文件中。你是怎么处理的?

However, it doesn't look very nice as I expecting the product from Microsoft would work more or less out of box without the need to use 3rd party hacks

原文件来自微软。它只是社区维护的。同样使用开源不是"third party hacks"。 Typescript 编译器支持使用定义文件。

Also, it looks like I will need to add this reference file to every file I will need to use node.js modules.

您可以将引用的所有这些定义文件聚合成一个'vendor.d.ts',然后这是您唯一需要引用的文件。此参考也可以代码生成:https://github.com/TypeStrong/grunt-ts/blob/master/README.md#transforms