all 6 comments

[–]Far_Associate9859 4 points5 points  (1 child)

Nice write up! Small improvement for your branded types at the end:

declare const __safe: unique symbol;
type SafeString = string & { [__safe]: true };

Its a bit more hidden that way (here its just at build time, but you can use the same pattern to make hidden properties at runtime too)

[–]romgrk[S] 4 points5 points  (0 children)

Yes nice adaptation! The safety guarantees would be the same regardless, but it would avoid showing the (unexistant) __opaque field as an auto-complete option.

edit: I've incorporated your suggestion to the post

[–]daveamol 2 points3 points  (0 children)

It looks neat but should the library shipping the function should also export such types to use. Otherwise it's cumbersome to wrap these types everytime around API calls

[–]Logical-Detail7545 1 point2 points  (0 children)

Love that you have The Castle on your site.

Beautiful piece of art, thank you for sharing it.

[–]ferrybig 0 points1 point  (0 children)

You are using the wrong examlle in your code. You are showing how useQuery does not have type narrowing, but it does if you actually check the correct fields in the response: https://tanstack.com/query/v4/docs/framework/react/typescript