Open nickluger opened 11 months ago
Maybe I got something wrong, but the data property returned by useTypedFetcher can be undefined. TS does not complain, while the runtime code fails.
data
useTypedFetcher
undefined
const { data } = useTypedFetcher<typeof loader>; console.log(data.foo); // error thrown, Cannot read properties of undefined etc.
Taking a look at the implementation:
export type TypedFetcherWithComponents<T> = Omit<FetcherWithComponents<T>, 'data'> & { data: UseDataFunctionReturn<T>; // ==> should be export type TypedFetcherWithComponents<T> = Omit<FetcherWithComponents<T>, 'data'> & { data: UseDataFunctionReturn<T> | undefined; // add undefined here };
Workaround
import type { FetcherWithComponents } from "@remix-run/react"; import type { UseDataFunctionReturn } from "remix-typedjson"; import { useTypedFetcher as useTypedFetcherOriginal } from "remix-typedjson"; export function useTypedFetcher<T>(): Omit<FetcherWithComponents<T>, "data"> & { data: UseDataFunctionReturn<T> | undefined; } { return useTypedFetcherOriginal<T>(); }
Maybe I got something wrong, but the
data
property returned byuseTypedFetcher
can beundefined
. TS does not complain, while the runtime code fails.Taking a look at the implementation:
Workaround