Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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))
  }
})
like image 793
walnut_salami Avatar asked Feb 04 '20 11:02

walnut_salami


1 Answers

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)

like image 169
walnut_salami Avatar answered Jan 01 '23 07:01

walnut_salami