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

时间:2016-08-14 11:13:01

标签: javascript node.js typescript

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

  1. 我从Node存储库
  2. 下载了DefinitelyTyped
  3. 我创建了我的打字稿文件
  4. 导入定义
  5. 尝试使用它
  6. 结果是:

    /// <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模块。我在哪里可以找到类型?在定义文件中,我很难识别这些信息。

1 个答案:

答案 0 :(得分:17)

这是因为您正在使用require。使用import而不是它会识别并且还会给你很好的智能感知: - )

import * as http from "http"