-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
useLoaderData types broken if unstable_data is conditionally used #9826
Comments
The following works for us. Happy to make a PR. "Unwrap" naming could be improved. type Serializable =
| undefined
| null
| boolean
| string
| symbol
| number
| Serializable[]
| { [key: PropertyKey]: Serializable }
| bigint
| Date
| URL
| RegExp
| Error
| Map<Serializable, Serializable>
| Set<Serializable>
| Promise<Serializable>;
type DataFunctionReturnValue =
| Serializable
| DataWithResponseInit<Serializable>
| TypedDeferredData<Record<string, unknown>>
| TypedResponse<Record<string, unknown>>;
export type Unwrap<T extends DataFunctionReturnValue> =
T extends TypedDeferredData<infer D>
? D
: T extends TypedResponse<Record<string, unknown>>
? SerializeFrom<T>
: T extends DataWithResponseInit<infer D>
? D
: T;
export type Serialize<T extends Loader | Action> =
Awaited<ReturnType<T>> extends DataFunctionReturnValue
? Unwrap<Awaited<ReturnType<T>>>
: Awaited<ReturnType<T>>; |
This is resolved by #9999 and will be included in the next release 👍 |
🤖 Hello there, We just published version Thanks! |
🤖 Hello there, We just published version Thanks! |
@brophdawg11 I have same issue in remix v2.14.0 import { type LoaderFunctionArgs, data } from "@remix-run/node";
import { useLoaderData } from "@remix-run/react";
export const loader = async () => {
return data({
foo: "bar",
}, {
headers: {
"Set-Cookie": "Foo=Bar",
},
});
};
export default function Index() {
const loaderData = useLoaderData<typeof loader>();
// ^^^ The `loaderData` type is JsonifyObject<DataWithResponseInit<{ foo: string }>>
// Expect JsonifyObject<{ foo: string }>
} |
I was running into this too and what solved it for me was combining these two parts from Essential Typescript for React:
So something like: type LoaderData = Omit<Awaited<ReturnType<typeof loader>>, 'foo'> & {
foo: string
}
const loaderData = useLoaderData<LoaderData>(); I'm sure this will be patched at some point, but this seems to work in the meantime. Edit: I forgot to declare the module as well in declare module '@remix-run/node' {
interface Future {
v3_singleFetch: true
}
} Now |
Reproduction
With the following loader:
const { notification } = useLoaderData<typeof loader>();
returns a type issue. The issue exists because of the conditional use ofunstable_data
. If I remove the "isBot" block, the type issue is gone.System Info
Used Package Manager
pnpm
Expected Behavior
No type issue. In the use case above,
notification
would beNotificationType | null
Actual Behavior
Type issue attached above.
The text was updated successfully, but these errors were encountered: