Skip to content
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

Merged
merged 1 commit into from
Jun 21, 2024

Conversation

pcattori
Copy link
Contributor

No description provided.

Copy link

changeset-bot bot commented Jun 21, 2024

⚠️ No Changeset found

Latest commit: 7bee528

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@@ -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;
Copy link
Contributor Author

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] ?
Copy link
Contributor Author

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 :
Copy link
Contributor Author

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,
Copy link
Contributor Author

@pcattori pcattori Jun 21, 2024

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).

@pcattori pcattori merged commit 1ea1d90 into v7 Jun 21, 2024
5 checks passed
@pcattori pcattori deleted the pedro/define-route-allow-undefined-data branch June 21, 2024 20:37
@pcattori pcattori added the v7 label Jun 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant