tRPCttRPC
Powered by
MartinM
tRPC•3y ago
Martin

Using generics duplicates the types instead of referring to the existing types

Hi!

I'm creating a backend API using TRPC, where I'm encountering a slight problem. TL;DR; when defining procedures with a generic type, instead of referring to the existing type, it copies the definition of the existing type when building the types.

-
Further info:

I have defined many data models/types in a shared package that multiple projects can access. For example I might have the data model "User", which contains all necessary fields for a user, i.e. e-mail, name, phone number, etc. I have setup generic CRUD API endpoints/procedures that accept a type such a "User", and after retrieving it from the database, it appends a field "id" (referring to the unique ID from the database).

Pretty much just:
User & { id: string };
User & { id: string };
. However, when looking at the output, I get the entire
User
User
type, as seen in the first image attached. A workaround I found was by exporting the final type instead of passing on generics, see image 2 and 3, then you can see it easily takes the shared type and uses it, instead of re-creating the entire type.

The question is:
Is there a way to pass on generics like I want, instead of manually declaring shared types? If it doesn't use shared types, it drastically increases the output size, since instead of referring to the existing types, it duplicates them.

Attachments:
1st image: The output (d.ts files) when building the project RN.
2nd: The type that's exported/shared
3rd: The new, what-I-want output after building the project, but preferable without doing it manually.
image.png
image.png
image.png
tRPCJoin
Move Fast & Break Nothing. End-to-end typesafe APIs made easy.
5,015Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

What's the benefit of using the context instead of a direct import for the database connection?
Michael SchaufelbergerMMichael Schaufelberger / ❓-help
3y ago
How to infer types of a query
HussamHHussam / ❓-help
3y ago
How to share the types correctly?
Answer OverflowAAnswer Overflow / ❓-help
9mo ago
Hydration error when using useQuery instead of useSuspenseQuery when prefetching
Michael SchaufelbergerMMichael Schaufelberger / ❓-help
15mo ago