正如 Node.js 的文档所述,http.IncomingMessage
is a class ,不是接口(interface)。那么为什么在 DefinitelyTyped http.IncomingMessage
defined as interface ?
现在在我的代码中我不能这样做:
import * as http from 'http';
let Request = http.IncomingMessage;
TypeScript 错误:
[ts] Property 'IncomingMessage' does not exist on type 'typeof "http"'.
我做错了什么?
最佳答案
我看到确实不存在用于扩展 http.IncomingMessage
的 Node.js API。这种方式更像是 hack 而不是正常的扩展:
http.IncomingMessage.prototype.absoluteUri = function absoluteUri(path) {
// ...
};
我found this回答扩展http.IncomingMessage
:
var extendedRequest = {
get userAgent() {
this.request.headers['user-agent'];
}
}
createServerCallback(function (req, res) {
var request = Object.create(extendedRequest);
request.request = req;
});
关于node.js - 为什么在 DefinitelyTyped `http.IncomingMessage` 中定义为接口(interface),而不是类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41390781/