Created
February 1, 2022 10:38
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>>(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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)