From c7ddfaf7c17dae6e5e2c12ce042fc775a7932990 Mon Sep 17 00:00:00 2001 From: Zane Hitchcox Date: Thu, 18 Apr 2024 16:00:44 -0400 Subject: [PATCH] infer useMatchesData type using generic --- app/utils.ts | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/app/utils.ts b/app/utils.ts index f6da6bd0..58a755bd 100644 --- a/app/utils.ts +++ b/app/utils.ts @@ -1,3 +1,4 @@ +import { type SerializeFrom } from '@remix-run/node' import { useMatches } from "@remix-run/react"; import { useMemo } from "react"; @@ -33,17 +34,18 @@ export function safeRedirect( * @param {string} id The route id * @returns {JSON|undefined} The router data or undefined if not found */ -export function useMatchesData( - id: string, -): Record | undefined { - const matchingRoutes = useMatches(); - const route = useMemo( - () => matchingRoutes.find((route) => route.id === id), - [matchingRoutes, id], - ); - return route?.data as Record; +export function useMatchesData | undefined>( + id: string, +): SerializeFrom { + const matchingRoutes = useMatches() + const route = useMemo( + () => matchingRoutes.find(route => route.id === id), + [matchingRoutes, id], + ) + return route?.data as SerializeFrom } + function isUser(user: unknown): user is User { return ( user != null &&