'Koa + TypeScript: Property 'body' does not exist on type Request
I wanted to use koa & koa-bodyparser with TypeScript but whenever I access ctx.request.body
I get an error that body
doesn't exist on type Request
import Koa from 'koa'
import Router from 'koa-router'
import bodyparser from 'koa-bodyparser'
const app = new Koa()
const router = new Router()
const data = ['lorem', 'ipsum', 'dolor', 'sit', 'amet']
app.use(bodyparser())
router.post('/', (ctx, next) => {
const phrase = ctx.request.body; // Property 'body' does not exist on type Request
if (typeof phrase === 'string') {
ctx.response.body = data.filter(element => element.includes(phrase))
}
})
Solution 1:[1]
Run npm install --save-dev @types/koa-bodyparser
in a terminal while in the directory where your package.json is
This package contains types introduced by koa-bodyparser (such as request.body)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |