koajs / router

Router middleware for Koa. Maintained by @forwardemail and @ladjs.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[fix] ctx.request.body 不能设置类型标注

wuxu1230 opened this issue · comments

commented

为何只有 body 不能设置类型,难道只能使用
request.body as {...} 方式吗,希望解决下面的使用出现的类型丢失问题
image

源代码如下("@types/koa": "2.13.10", "@types/koa__router": "12.0.3")

import Router from "@koa/router"
const router = new Router()
interface KoaState {
    a: string
}
interface Context {
    request: {
        params: {
            id: string
        }
        body: {
            c: string
        }
        body2: {
            d: string
        }
    }
}
interface ResponseBody {
    b: string
}

router.post<KoaState, Context, ResponseBody>("/:id", async (ctx) => {
    const {state, request, response} = ctx
    const a = state.a
    const id = request.params.id
    const c = request.body.c
    const d = request.body2.d
    const b = response.body.b
})