Skip to content

Commit

Permalink
fix(@remix-run/react): undefinedoptionals should preserve null types
Browse files Browse the repository at this point in the history
  • Loading branch information
pcattori committed Jul 29, 2022
1 parent 696290e commit 6174e3b
Showing 1 changed file with 11 additions and 3 deletions.
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 6174e3b

Please sign in to comment.