fgnass / typed-rpc

Lightweight JSON-RPC solution for TypeScript projects
121 stars 8 forks source link

Return type is wrong for "complex" objects #11

Closed ptrxyz closed 1 year ago

ptrxyz commented 1 year ago

First, great library, just perfect for small projects! No overload, no clutter, straight to the point :)

However I find when you return

from your service things go wrong a bit: since we rely on type MyService = typeof MyService, IntelliSense and TS think you actually return a Date/Map/Set object. However JSON serialization breaks this.

This is quite dangerous in my opinion since you can easily break you API without any hint from tsc/the IDE.

Is there a simple fix for it? Maybe we can simply have the types adjusted? However this would still make it so that Sets/Maps are empty when passed through JSON.stringify. Maybe we can use devalue or another library to handle it?

ptrxyz commented 1 year ago

Maybe this would be of any help? https://github.com/blitz-js/superjson

fgnass commented 1 year ago

Thanks for your feedback, that's a very good point! I just published v4.0.0 which enforces stricter types on the service. I decided against using superjson or similar approaches for now. The new typings will hopefully help to guard devs from the issues you mentioned.

fgnass commented 1 month ago

FYI: Support for superjson has been added in v6.0.0

ptrxyz commented 3 weeks ago

Awesome! Thanks a bunch!