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 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions contributors.yml
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
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);
});
});
23 changes: 18 additions & 5 deletions packages/remix-react/components.tsx
Expand Up @@ -20,6 +20,7 @@ import {
useResolvedPath,
} from "react-router-dom";
import type { LinkProps, NavLinkProps } from "react-router-dom";
import type { Merge } from "type-fest";

import type { AppData, FormEncType, FormMethod } from "./data";
import type { EntryContext, AssetsManifest } from "./entry";
Expand Down Expand Up @@ -1336,6 +1337,7 @@ type JsonPrimitives =
| Boolean
| null;
type NonJsonPrimitives = undefined | Function | symbol;

type SerializeType<T> = T extends JsonPrimitives
? T
: T extends NonJsonPrimitives
Expand All @@ -1353,13 +1355,24 @@ 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> = Merge<
{
[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]>;
}
>;

export type UseDataFunctionReturn<T extends DataOrFunction> = T extends (
...args: any[]
) => infer Output
Expand Down
3 changes: 2 additions & 1 deletion packages/remix-react/package.json
Expand Up @@ -17,7 +17,8 @@
"module": "dist/esm/index.js",
"dependencies": {
"history": "^5.3.0",
"react-router-dom": "^6.2.2"
"react-router-dom": "^6.2.2",
"type-fest": "^2.17.0"
},
"devDependencies": {
"@testing-library/jest-dom": "^5.16.2",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Expand Up @@ -12127,6 +12127,11 @@ type-fest@^2.16.0:
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-2.16.0.tgz#1250fbd64dafaf4c8e405e393ef3fb16d9651db2"
integrity sha512-qpaThT2HQkFb83gMOrdKVsfCN7LKxP26Yq+smPzY1FqoHRjqmjqHXA7n5Gkxi8efirtbeEUxzfEdePthQWCuHw==

type-fest@^2.17.0:
version "2.17.0"
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-2.17.0.tgz#c677030ce61e5be0c90c077d52571eb73c506ea9"
integrity sha512-U+g3/JVXnOki1kLSc+xZGPRll3Ah9u2VIG6Sn9iH9YX6UkPERmt6O/0fIyTgsd2/whV0+gAaHAg8fz6sG1QzMA==

type-is@^1.6.18, type-is@~1.6.18:
version "1.6.18"
resolved "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz"
Expand Down