Skip to content

Add generic type parameter to json() methods #3

@max-lt

Description

@max-lt

Proposal

Add an optional generic type parameter to json() methods on Response and Request for better TypeScript ergonomics.

Current signature

json(): Promise<unknown>;

Proposed signature

json<T = unknown>(): Promise<T>;

Benefits

  • Allows response.json<MyType>() instead of (await response.json()) as MyType
  • Default unknown maintains current strict behavior
  • Fully backward compatible - existing code continues to work
  • Consistent with other OpenWorkers APIs like KV.get<T>() and DB.query<T>()

Example

// Current (still works)
const data = (await response.json()) as { text: string };

// Proposed (new option)
const data = await response.json<{ text: string }>();

Affected types

  • Response.json() (line 139 in fetch.d.ts)
  • Request.json() (line 230 in fetch.d.ts)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions