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

Is it possible to have data parameter typed in meta function export? #6

Closed shripadk closed 1 year ago

shripadk commented 2 years ago

Firstly, thank you for the awesome library and for bringing end-to-end type safety to Remix.

I have a problem that is specific to the exported meta function from a route.

Have a posts/$slug route which has a loader which exports few posts and rendered markdown in html format. With remix-typedjson it looks something like this:

export const loader = async ({ params }: LoaderArgs) => {
  invariant(params.slug, "params.slug is required")

  const post = await getPost(params.slug)

  invariant(post, `Post not found: ${params.slug}`)

  const html = marked(post.markdown)

  return typedjson({
    post,
    html,
  })
}

But when I want to access the returned data in the meta function to add a title for my page, I obviously don't get the types as MetaFunction by default has data set to any:

export const meta: MetaFunction = ({ data }) => {
  console.log("Data:", data)

  return {
    title: `Posts | ${data.post.title}`,
  }
}

Is it possible to type data returned from loader in the meta function?

kiliman commented 2 years ago

Thanks. I’ll see about adding a meta export as well.