【问题标题】:RxJS Http Server with typescript compile errors带有 typescript 编译错误的 RxJS Http Server
【发布时间】:2021-04-14 20:44:45
【问题描述】:

我是 rxjs 的新手,你能告诉我下面有什么问题吗?

interface IHTTP {
    req: IncomingMessage;
    res: ServerResponse;
    handler?: Promise<any>;
}
server = http.createServer();
  let request$: Observable<any>;
  request$ = fromEvent(server, 'request').pipe(
    map(([req, res]: [IncomingMessage, ServerResponse]): IHTTP => {
        return { req, res } as IHTTP;
      }
    )
  );

编译错误:

TS2345: Argument of type 'OperatorFunction<[IncomingMessage, ServerResponse], IHTTP>' is not assignable to parameter of type 'OperatorFunction<unknown, IHTTP>'.   Type 'unknown' is not assignable to type '[IncomingMessage, ServerResponse]'.

【问题讨论】:

  • 这与 Angular 和 http 客户端无关,正如我在标题中所说,它是用于 HTTP 服务器的。 @RichardRublev

标签: node.js typescript http rxjs


【解决方案1】:

我发现我必须将fromEvent的第一个参数定义为:

server as FromEventTarget<[http.IncomingMessage, http.ServerResponse]>

否则 Typescript 认为它是 FromEventTarget&lt;unknown&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-21
    • 2017-11-21
    • 2019-09-22
    • 2017-04-02
    • 2018-06-09
    • 2014-03-07
    相关资源
    最近更新 更多