如何在 TypeScript 中使用 Node `http` 模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38941636/
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
How to use Node `http` module in TypeScript
提问by Andry
I need to write a server in TypeScript and Node.
我需要用 TypeScript 和 Node.js 编写一个服务器。
- I downloaded Nodefrom DefinitelyTypedrepository
- I created my typescript file
- Imported the definition
- Tries to use it
The result is:
结果是:
/// <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'
}
}
}
But I cannot understand how I can reference the http
module. Where can I find the types? In the definition file i am having hard time recognizing this information.
但我无法理解如何引用该http
模块。我在哪里可以找到这些类型?在定义文件中,我很难识别这些信息。
回答by Jorawar Singh
It's because of you are using require
. use import
instead it will recognize and also will give you nice intellisense :-)
那是因为你正在使用require
. 使用import
而是将承认并也会给你不错的智能感知:-)
import * as http from "http"