kiliman / remix-typedjson

This package is a replacement for superjson to use in your Remix app. It handles a subset of types that `superjson` supports, but is faster and smaller.
MIT License
435 stars 19 forks source link

Problem with deep objects #2

Closed frontsideair closed 2 years ago

frontsideair commented 2 years ago

When I wrap the loader data in another object, it fails while deserializing. Here's a reproduction: https://stackblitz.com/edit/github-gbcwza?file=app%2Froutes%2Ftest.tsx

The code:

  return typedjson(
    { data: { greeting: "hello", today: new Date() } },
  );
kiliman commented 2 years ago

It actually works as designed. The problem is that you modified the loader return, but not how you used it in the route. It's basically telling you that you should be using

loaderData.data.today.toLocaleDateString()

If you think about it, that's pretty cool!

image
frontsideair commented 2 years ago

You're absolutely correct about this case, I botched the repro while trying to shrink it. The following is the correct reproduction and I removed the <p> to reduce confusion.

  return typedjson(
    { data: [{ greeting: "hello", today: new Date() }] },
  );

I also updated the StackBlitz, it should be visible with the same url.

kiliman commented 2 years ago

Ugh. You are correct. I copied the loader to the resource route so I could see what the actual payload was.

image

Meta should have been: { "data.0.today": "date" } https://github-gbcwza-vh4vsh--3000.local.webcontainer.io/resource

kiliman commented 2 years ago

This works now in v0.0.7. Thanks for the bug report!

https://stackblitz.com/edit/github-gbcwza-9pxd9e?file=app%2Froutes%2Ftest.tsx