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

fix(@remix-run/react): preserve null properties for useLoaderData and useActionData inference #3879

Merged
merged 2 commits into from
Jul 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .changeset/nervous-walls-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"remix": patch
"@remix-run/react": patch
---

Fix inferred types for `useLoaderData` and `useActionData` to preserve `null`s.

Previously, `null`s were being replaced by `never`s due to usage of `NonNullable` in `UndefinedOptionals`.
Properties that aren't unions with `undefined` are now kept as-is, while properties that _do_ include `undefined`
are still made optional, but _only_ remove `undefined` from the property type whereas `NonNullable` also removed `null`s.
14 changes: 11 additions & 3 deletions packages/remix-react/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1364,12 +1364,20 @@ type SerializeObject<T> = {
>;
};

type UndefinedOptionals<T> = Merge<
/*
* For an object T, if it has any properties that are a union with `undefined`,
* make those into optional properties instead.
*
* Example: { a: string | undefined} --> { a?: string}
*/
type UndefinedOptionals<T extends object> = Merge<
{
[k in keyof T as undefined extends T[k] ? never : k]: NonNullable<T[k]>;
// Property is not a union with `undefined`, keep as-is
[k in keyof T as undefined extends T[k] ? never : k]: T[k];
},
{
[k in keyof T as undefined extends T[k] ? k : never]?: NonNullable<T[k]>;
// Property _is_ a union with `defined`. Set as optional (via `?`) and remove `undefined` from the union
[k in keyof T as undefined extends T[k] ? k : never]?: Exclude<T[k], undefined>;
}
>;

Expand Down