node.js - 为什么在 DefinitelyTyped `http.IncomingMessage` 中定义为接口(interface),而不是类?

标签 node.js typescript

正如 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/

相关文章:

带有自身类类型的 TypeScript 克隆自身实例

typescript - 在 TypeScript 中处理动态 `.apply()` 使用的正确方法是什么?

javascript - 如何在 Angular 中使用 Canvas

javascript - 如何将一个集合字段合并到另一个集合字段而不重复

node.js - 如何安装nodejs 12版本

javascript - 将 ts 用于 tsx react 文件

typescript - 如何禁用生成 "exports.__esModule = true;"和 "require("lib");"

node.js - 使用 passport.js 推特登录重新登录用户

node.js - 使用 node.js 的服务器端 mustache.js 示例

node.js - 错误 : EXDEV: cross-device link not permitted, 在 Ubuntu 16.04 LTS 上重命名 '/tmp/