bodyparser icon indicating copy to clipboard operation
bodyparser copied to clipboard

Why is body typed as string? gives tsc compilation errors

Open maapteh opened this issue 3 years ago • 4 comments

Why is this library not self typed. Now the type definition somewhere else is like:

declare module 'koa' {
    interface Request {
        body: string | Record<string, unknown>;
        rawBody: string;
    }
}

It seems i have to drop it at https://www.npmjs.com/package/@types/koa-bodyparser

maapteh avatar Jul 09 '21 07:07 maapteh

Hello there, can you share how did you manage to resolve this issue?

tsejerome avatar Jul 27 '21 07:07 tsejerome

Hi, i pinned it to version 4.3.0 untill its fixed correctly :)

maapteh avatar Jul 27 '21 13:07 maapteh

Having the same issue... pinning @types/koa-bodyparser at 4.3.0 resolves it for now - but I'd like to see this fixed!

jtylerroth avatar Aug 03 '21 14:08 jtylerroth

For now I use only application/json bodies so I did that:

const myInternalVar:MyInternalType = JSON.parse(ctx.request.rawBody);

It's ugly but it works.

nicolasespiau avatar Aug 03 '21 23:08 nicolasespiau

Duplicated #150

Hey guys, I would mention that I have re-created the module with TS so no need to @types/koa-bodyparser in the future (once the PR merged).

For a quick solution about the typing behave, you can override the request signature like this

declare module "koa" {
   interface Request {
       body?: Record<string, unknown>; // BTW, you can override it to any type you want ... 
       rawBody: string;
   }
}

3imed-jaberi avatar Mar 31 '23 16:03 3imed-jaberi