【问题标题】:Typescript and Koa BaseContext Issue打字稿和 Koa BaseContext 问题
【发布时间】:2020-02-29 08:03:36
【问题描述】:

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

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

import { BaseContext } from 'koa'



export function handle(ctx: BaseContext) {
    ctx.body = ctx.request.body
}

tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "esModuleInterop": true,
    "target": "es2017",
    "noImplicitAny": true,
    "moduleResolution": "node",
    "sourceMap": true,
    "outDir": "dist",
    "baseUrl": "."
  },
  "include": ["src/**/*", "env/**/*"]
}

【问题讨论】:

    标签: node.js typescript koa


    【解决方案1】:

    在这里快速查看实际类型:

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

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

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

    【讨论】:

    • 我以前用 const file = ctx.request.file 用 multer 获取文件。我现在该怎么做?当我添加 // @ts-ignore 我能够运行代码。
    • 我不知道是什么增加了file 属性,但它不在基础 Koa 接口中。如果之前的 Context 对象被键入为 any,它可能之前工作过,但我不确定。
    • 对于单个文件,它是 ctx.file。感谢您的帮助!
    • 在某个 koa 版本之后 api 发生了变化
    猜你喜欢
    • 2019-07-19
    • 2017-09-26
    • 2021-06-19
    • 2020-09-25
    • 2021-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-05
    相关资源
    最近更新 更多