Skip to content

Commit

Permalink
fix(@remix-run/react): preserve null properties for useLoaderData
Browse files Browse the repository at this point in the history
… and `useActionData` inference (#3879)
  • Loading branch information
pcattori authored Jul 30, 2022
1 parent a2914cd commit 2b73608
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
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

0 comments on commit 2b73608

Please sign in to comment.