Typescript 和 Koa BaseContext 问题

F4l*_*0ut 0 node.js typescript koa

Typescript 抛出请求在 BaseContext 类型中不存在的错误信息。那不可能是因为我以前使用过代码并且它有效。

类型“BaseContext”上不存在属性“请求”。

import { BaseContext } from 'koa'



export function handle(ctx: BaseContext) {
    ctx.body = ctx.request.body
}
Run Code Online (Sandbox Code Playgroud)

配置文件

{
  "compilerOptions": {
    "module": "commonjs",
    "esModuleInterop": true,
    "target": "es2017",
    "noImplicitAny": true,
    "moduleResolution": "node",
    "sourceMap": true,
    "outDir": "dist",
    "baseUrl": "."
  },
  "include": ["src/**/*", "env/**/*"]
}
Run Code Online (Sandbox Code Playgroud)

Eve*_*ert 6

快速浏览一下这里的实际类型:

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/koa/index.d.ts

看起来这些属性在BaseContext. 最近似乎对此进行了一些更改。

我可能弄错了,但我相信使用的正确类型是Context,而不是BaseContext。这至少一直对我有用。