Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[libdom] Allow Response / Request to be generics #52777

Open
5 tasks done
karlhorky opened this issue Feb 15, 2023 · 3 comments
Open
5 tasks done

[libdom] Allow Response / Request to be generics #52777

karlhorky opened this issue Feb 15, 2023 · 3 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@karlhorky
Copy link
Contributor

karlhorky commented Feb 15, 2023

Suggestion

πŸ” Search Terms

request, response, libdom, generic, generics, type parameters

βœ… 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

Similar to the FormData request by @wesbos in #43797, it would be great if the Request and Response globals could also accept generic type parameters.

This will become more relevant as more frameworks continue to increase their usage of these standard globals, also in Node.js.

Consider the following example of an App Route handler in Next.js (handling a POST request to an API route):

export async function POST(request: Request): Response {
  const body = await request.json();
     // ^? const body: any ❌
  return new Response(JSON.stringify({
    abc: 123, // ❌ POST function return type cannot check type of response body object
  }))
}

What would be amazing would be something similar to this:

type RequestBody = { zzz: number };
type ResponseBody = { abc: number };

export async function POST(request: Request<RequestBody>): Response<ResponseBody> {
  const body = await request.json();
     // ^? const body: { zzz: number } βœ…
  return new Response(JSON.stringify({
    abc: 123, // βœ… type checked
  }))
}

πŸ“ƒ Motivating Example

The second example in the Suggestion section above.

πŸ’» Use Cases

For typing the response body of API routes

Prior Art

@types/express: Request and Response types - which both accept a generic type parameter.

Workaround

Response

Wasn't able to find a nice workaround for typing the body of a Response yet.

Request

Using Type Assertion / casting via as:

type RequestBody = { zzz: string };

export async function POST(request: Request) {
  const body = await request.json() as RequestBody;

Caveats

  1. Arguably, the Request body is unknown until runtime, so this is better handled instead with Zod or some other runtime validation. But for quick/simple projects without this validation, it's nice to be able to type the request body type.
@MartinJohns
Copy link
Contributor

IMO the same reason applies here: #33037 (comment)

Same thing why JSON.parse() is not generic.

@karlhorky
Copy link
Contributor Author

JSON.parse() is only one part of the equation - the parsed request body. That is why I wrote the caveat in the issue above:

  1. Arguably, the Request body is unknown until runtime, so this is better handled instead with Zod or some other runtime validation. But for quick/simple projects without this validation, it's nice to be able to type the request body type.

Type checking the return value of a function (the Response part of this proposal) is a statically analyzable way to enforce the response shapes of API handlers (eg. especially useful with the new Response.json() static method) and does not have anything to do with the "JSON data off the wire" mentioned in your linked comment.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Feb 24, 2023
@k1eu
Copy link

k1eu commented Aug 31, 2024

@karlhorky You might like this. (I saw you on my TypedFormData package! :) I build a very small wrapper around fetch, Request and Response for this particular issues :)
Ofc it's fully compatible with regular interfaces. You can check it out here! https://github.com/k1eu/typed-request

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants