Skip to content

Types for Response.json() Static Method #52841

Closed
@karlhorky

Description

@karlhorky

Suggestion

🔍 Search Terms

response, responses, static method, json, Response.json()

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Now that multiple JavaScript runtimes (Chromium, Node.js v18 LTS, Bun, etc) have implemented it, it would be great to get types for the Response.json() static method (Chrome Platform Status, Mozilla Bugzilla, Response.json() section in Fetch spec).

Since Response objects are more commonly used on the backend, maybe backend runtimes can be considered as valid signals that this is ready for inclusion in TypeScript.

📃 Motivating Example

Response.json({abc: 123}, {status: 400});

Playground

Screenshot 2023-02-18 at 15 33 32

💻 Use Cases

A shortcut for the following code (more verbose / error prone):

new Response(JSON.stringify({ abc: 123 }), {
  status: 400,
  headers: {
    'Content-Type': 'application/json',
  },
});

👀 See also

This has a companion issue + PR in the microsoft/TypeScript-DOM-lib-generator repo over here:

cc @lucacasonato @balazsorban44 @leerob @cramforce @jasnell

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions