#Overriding the type of `Response.json`

1 messages · Page 1 of 1 (latest)

silver moss
#

Hey! I’m using the following pattern to add some type safety to my API responses:

type ApiReturn =
  | {}
  | {}
  | {};

// ...

return Response.json({
  // ...
} satisfies ApiReturn);

Is it possible to override the type of Response.json so it only accepts values matching the ApiReturn type? I played around with this idea a bit but couldn't get it to work.

This might not be specific to Bun's types, but I saw that they do some trickery with Bun.__internal.UseLibDomIfAvailable so I presume it would require a unique solution.

solar slate
#

@desert magnet

desert magnet
#

Unfortunately it's not possible @silver moss

#

The issue is that TypeScript doesn't let us extend the declaration of a variable, there's no concept of "interface merging" (or something that looks like interface merging) there

#

the solution would be for the typescript dom generator to create var Response: ResponseConstructor (Which is what my pull request did) but the maintainers did not like it, unfortunately.

silver moss
desert magnet
#

yeah😢

silver moss
#

This is unfortunately way above my TS knowledge, thanks for the response though

desert magnet
#

my recommendation for you is a helper around Response.json

like response({...})

solar slate
#

response<smth>() would be fun lol, but idk if it would work the same as satisfies

silver moss
desert magnet
#

cool!

desert magnet
#

@silver moss Feel free to leave a comment explaining on the closed PR if you feel comfortable doing so, explaining that you need this feature/want to extend the Response constructor, but are unable to because of this limitation currently

#

The alternative/proposed solution from a few years ago isn't really moving much 😢

silver moss
#

I was already thinking of doing that, but I then discarded the idea because I thought that if the needs of a Bun maintainer were dismissed, my needs wouldn't even be considered.

desert magnet
silver moss
#

I can even ask some friends to also comment on the issue 😛 - idk if that's frowned upon in the OSS world

#

On a related note, I've been stuck on a TS issue. I came up with a couple of workarounds, but I could really use someone with deep TS knowledge to help me pick the right one. It's not specific to @types/bun, but I would appreciate it if you could comment on it.

I put a minimal example and the list of my ideas here.

#

I understand if you don't have the time / don't want to, but I thought I'd shoot my shot and ask

desert magnet
desert magnet
desert magnet
#

@silver moss

#

I didn't really check the solution much but just adding a root tsconfig fixed it - note that there was one issue in either @types/node or @types/bun which you'd be able to ignore by re-enabling skipLibCheck

#

the tsconfig I copy pasted it from alistair.blog/strict-tsconfig

#

I think this is something to do with how tsconfig thinks about monorepos. I'm not quite sure. the differences are very subtle

silver moss
silver moss
silver moss
desert magnet
#

They are the default for most package managers still in most cases

silver moss
#

I will move forward with this because it's literally a 1-line fix and I can just move on, but I will eventually read through the TS docs and try to make it work with isolated installs.

#

Thanks for your help!

desert magnet
#

The solution might be something to do with project references @silver moss

#

but im not sure

#

I actually dont know what the real problem is yet, i just know that I did something, ran tsc and it worked

#

😅

silver moss
#

Will give it a read when it's time for the inevitable refactor. 🤓