Search code examples
node.jstypescriptkoa

Typescript and Koa BaseContext Issue


Typescript throws the error message that request does not exist in the type BaseContext. That can't be because I've used the code before and it worked.

Property 'request' does not exist on type '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/**/*"]
}

Solution

  • Having a quick look at the actual types here:

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

    It kinda looks like these properties don't exist on BaseContext. It also does seem like some changes were recently made to this.

    I might be mistaken, but I believe the correct type to use is Context, not BaseContext. This has at least always worked for me.