Skip to content

Instantly share code, notes, and snippets.

@merelinguist
Created February 1, 2022 10:38
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save merelinguist/350be12b8d1c492c40491819727c9283 to your computer and use it in GitHub Desktop.
Save merelinguist/350be12b8d1c492c40491819727c9283 to your computer and use it in GitHub Desktop.
Transforms any Date objects from your Remix loaders into strings for a more accurate representation of Remix's deserialisation
import { useLoaderData as useRemixLoaderData } from "remix";
type Nullable<Type> = Type | undefined | null;
type Deserialized<Data> = {
[Key in keyof Data]: Data[Key] extends Nullable<{ [key: string]: unknown }>
? Deserialized<Data[Key]>
: Data[Key] extends Date
? string
: Data[Key];
};
export const useLoaderData = <Data>(): Deserialized<Data> =>
useRemixLoaderData<Deserialized<Data>>();
@mmtftr
Copy link

mmtftr commented May 4, 2022

Honestly, remix should consider adding this as a note in their documentation so people know about it. It's a simple enough fact that someone might get wrong (especially if they're using Prisma types which contain Dates on the serverside)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment