如何在TypeScript中使用Node`http`模块

我需要在TypeScript和Node中编写一个服务器。

  1. 我从DefinitelyTyped存储库下载了Node
  2. 我创build了我的打字稿文件
  3. 导入定义
  4. 尝试使用它

结果是:

/// <reference path="definitions/commonjs.d.ts" /> /// <reference path="definitions/node.d.ts" /> var http = require("http"); namespace MyProj { export class Server { public run() { var server = http.createServer(); // TypeScript does not recognize 'http' } } } 

但我不明白我怎么可以参考http模块。 我在哪里可以findtypes? 在定义文件中,我很难识别这些信息。

这是因为你正在使用require 。 使用import它会识别,也会给你很好的intellisense 🙂

 import * as http from "http"