Martin
Martin2y ago

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 };. However, when looking at the output, I get the entire 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.
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?