router
router copied to clipboard
[fix] ctx.request.body 不能设置类型标注
为何只有 body 不能设置类型,难道只能使用
request.body as {...}
方式吗,希望解决下面的使用出现的类型丢失问题
源代码如下("@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
})