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

And*_*dry 8 javascript node.js typescript

我需要在TypeScript和Node中编写服务器.

  1. 我从DefinitelyTyped存储库下载了Node
  2. 我创建了我的打字稿文件
  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'
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但我无法理解我如何引用该http模块.我在哪里可以找到类型?在定义文件中,我很难识别这些信息.

Jor*_*ngh 24

这是因为你正在使用require.使用import而是将承认并也会给你不错的智能感知:-)

import * as http from "http"
Run Code Online (Sandbox Code Playgroud)