koajs / bodyparser

Koa body parsing middleware
MIT License
1.3k stars 116 forks source link

Why is body typed as string? gives tsc compilation errors #142

Closed maapteh closed 1 year ago

maapteh commented 3 years ago

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

tsejerome commented 2 years ago

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

maapteh commented 2 years ago

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

jtylerroth commented 2 years ago

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

nicolasespiau commented 2 years ago

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.

3imed-jaberi commented 1 year ago

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;
   }
}