Skip to content

[fix] Incorrect TypeScript types for request.body in v7 #245

@jrnail23

Description

@jrnail23

request.body types are currently defined as

declare module 'koa' {
    interface Request {
        body?: {
            [key: string]: unknown;
        } | string;
        rawBody?: string;
        files?: ScalarOrArrayFiles;
    }
}

JSON bodies do not necessarily need to be a string-keyed object. They can also be arrays.

If you're going to add a type here, perhaps something like type-fest's JsonValue type would be more appropriate:

/**
Matches a JSON object.

This type can be useful to enforce some input to be JSON-compatible or as a super-type to be extended from. Don't use this as a direct return type as the user would have to double-cast it: `jsonObject as unknown as CustomResponse`. Instead, you could extend your CustomResponse type from it to ensure your type only uses JSON-compatible types: `interface CustomResponse extends JsonObject { … }`.

@category JSON
*/
export type JsonObject = {[Key in string]: JsonValue};

/**
Matches a JSON array.

@category JSON
*/
export type JsonArray = JsonValue[] | readonly JsonValue[];

/**
Matches any valid JSON primitive value.

@category JSON
*/
export type JsonPrimitive = string | number | boolean | null;

/**
Matches any valid JSON value.

@see `Jsonify` if you need to transform a type to one that is assignable to `JsonValue`.

@category JSON
*/
export type JsonValue = JsonPrimitive | JsonObject | JsonArray;

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions