Open michaelgmcd opened 1 month ago
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>>;
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.