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-react): support undefined unions as optional keys in UseDataFunctionReturn type #3766

Merged
merged 4 commits into from
Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@
- isaacrmoreno
- ishan-me
- IshanKBG
- itsMapleLeaf
- jacob-ebey
- JacobParis
- jakewtaylor
Expand Down
10 changes: 10 additions & 0 deletions packages/remix-react/__tests__/hook-types-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,14 @@ describe("type serializer", () => {
type response = UseDataFunctionReturn<Loader>;
isEqual<response, { arg: string }>(true);
});

it("makes keys optional if the value is undefined", () => {
type AppData = {
arg1: string;
arg2: number | undefined;
arg3: undefined;
itsMapleLeaf marked this conversation as resolved.
Show resolved Hide resolved
};
type response = UseDataFunctionReturn<AppData>;
isEqual<response, { arg1: string; arg2?: number }>(true);
});
});
19 changes: 14 additions & 5 deletions packages/remix-react/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1336,6 +1336,7 @@ type JsonPrimitives =
| Boolean
| null;
type NonJsonPrimitives = undefined | Function | symbol;

type SerializeType<T> = T extends JsonPrimitives
? T
: T extends NonJsonPrimitives
Expand All @@ -1353,13 +1354,21 @@ type SerializeType<T> = T extends JsonPrimitives
: T extends (infer U)[]
? (U extends NonJsonPrimitives ? null : SerializeType<U>)[]
: T extends object
? {
[k in keyof T as T[k] extends NonJsonPrimitives
? never
: k]: SerializeType<T[k]>;
}
? SerializeObject<UndefinedOptionals<T>>
: never;

type SerializeObject<T> = {
[k in keyof T as T[k] extends NonJsonPrimitives ? never : k]: SerializeType<
T[k]
>;
};

type UndefinedOptionals<T> = {
[k in keyof T as undefined extends T[k] ? never : k]: NonNullable<T[k]>;
} & {
[k in keyof T as undefined extends T[k] ? k : never]?: NonNullable<T[k]>;
};
itsMapleLeaf marked this conversation as resolved.
Show resolved Hide resolved

export type UseDataFunctionReturn<T extends DataOrFunction> = T extends (
...args: any[]
) => infer Output
Expand Down