#Infer type in child

1 messages · Page 1 of 1 (latest)

bronze musk
#

I have this component that fetches the data using drizzle, the type of data is inferred and everything is fine in the parent, my question is how to transfer the type to the child without having to write the type by hand?

coarse wedge
#

I would generally not recommend that: type inference is a good way to get types from the loader into the route, but then each component should specify just the types it needs and then if the schema changes serverside, you get the Typescript errors at the point where you pass the props down, which is the right place for them

if you derive the types of your components from the database, then you won't get those type errors on your props, you'll get them in all the different places that you try to use the data inside the component, which can be a lot of hard to decipher errors

#

But the helper SerializedFrom<typeof loader>["posts"][number] should return the specific derived type of one of your posts

#

Another option would be to assemble your desired type from the drizzle schemas like Post & { comments: Array<Comment & { reactions: Array<Reaction>}>}> which is sort of halfway between the approaches

#

then it's not too tightly coupled to the query: you can consume the component from different routes that use slightly different queries to get the same data, but there's still some defined requirements in the component