Wrong type inference from beforeLoad/loader when using complex objects in return
I've added all the necessary types and declarations, started the fresh new project. I want to prefetch the product from an api and i have all the necessary types infered.
Here's an example of the code:
7 Replies
ratty-blushOP•3w ago
and when returning the whole product object i get this error (need to split this into multiple messages):
as for returning only title (for example) it infers the type just fine. That's why i think that there's something wrong only on some more complex types
Here's an example of the type that works when being returned from the loader: https://github.com/medusajs/medusa/blob/develop/www/utils/generated/oas-output/schemas/BaseRegion.ts
and here's an example of the current one that doesn't:
https://github.com/medusajs/medusa/blob/develop/www/utils/generated/oas-output/schemas/StoreProduct.ts
Also, i have tried prefetching a different data (there's also metadata object inside that data), and again the same error (i'll copy last few lines only):
wise-white•3w ago
Pretty sure the problem is the
unknown types since TS Start can't confidently serialize an unknown value.wise-white•3w ago
yep exactly
unknown could be a non serializable thing at runtime, hence the type errorratty-blushOP•3w ago
thanks for the reply. Then what would be the best course of action here? Reporting this to the package that i'm using doesn't seem right just because unknown can be and is used in many packages or should tanstack implement some kind of workaround/fix for this?
I'm asking this just becase i ignored the error, typed the data myself and it works just fine, and to not use something just because tere's a type missmatch seems like a wrong thing to do
wise-white•3w ago
The problem isn't really a type mismatch. The library you use most likely types it as
unknown since it is generic and they don't know what data you have, and Tanstack Start can't assume either. That leaves you, the developer, that knows what type your data has. That said, overriding the unknown type to something concrete, is the correct way to do it.wise-white•2w ago
absolutely
we are however looking into ways to let you opt out of this type check, on your own risk of course 😄
ratty-blushOP•2w ago
haha i get it, i mean, you practically opt out by typing it on your own, it's just annoying but still works 😄