-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
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
defineRoute: allow undefined data to be returned from loaders and actions #11708
Conversation
|
@@ -66,7 +64,7 @@ type Params<Param extends string> = Pretty< | |||
& {[K in Param as K extends `${infer P}?` ? P : never]?: string} | |||
> | |||
|
|||
type IsDefined<T> = undefined extends T ? false : true; | |||
type IsDefined<T> = Equal<T, undefined> extends true ? false : true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before we checked if undefined
extended T
, which is true for unions containing undefined
. Now we check if T
is exactly undefined
. This works since we now default all of the data types in defineRoute
and defineRootRoute
generics to be undefined
(Data | undefined = undefined
)
@@ -75,13 +73,13 @@ type _LoaderData< | |||
ClientLoaderHydrate extends boolean, | |||
HydrateFallback, | |||
> = Awaited< | |||
[IsDefined<HydrateFallback>, ClientLoaderHydrate] extends [true, true] ? | |||
[undefined extends HydrateFallback ? false : true, ClientLoaderHydrate] extends [true, true] ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unlike for Data
types (e.g. ServerLoaderData
), its correct to check for undefined extends
for HydrateFallback
since HydrateFallback
is a top-level value of defineRoute
whereas the things like ServerLoaderData
are inferred from the return type of a top-level value like serverLoader
.
IsDefined<ClientLoaderData> extends true ? ClientLoaderData : | ||
undefined | ||
: | ||
[IsDefined<ClientLoaderData>, IsDefined<ServerLoaderData>] extends [true, true] ? ServerLoaderData | ClientLoaderData : | ||
IsDefined<ClientLoaderData> extends true ? | ||
IsDefined<ClientLoaderHydrate> extends true ? ClientLoaderData : | ||
ClientLoaderHydrate extends true ? ClientLoaderData : |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was a bug that didn't show up until I added tests for returning undefined
. In short ClientLoaderHydrate
is a boolean
not a data type, so it should be checked directly.
ClientLoaderHydrate extends boolean, | ||
HydrateFallback extends HydrateFallbackComponent<Param> | undefined, | ||
ServerLoaderData extends Data | undefined, | ||
ClientLoaderData extends Data | undefined, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moving these for consistency with defineRoute
and defineRootRoute
order (which needed to change since generics with defaults have to go at the end).
No description provided.