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

SharedUnionFieldsDeep with undefined in Union #983

Open
ocadoret opened this issue Nov 18, 2024 · 3 comments · May be fixed by #988
Open

SharedUnionFieldsDeep with undefined in Union #983

ocadoret opened this issue Nov 18, 2024 · 3 comments · May be fixed by #988
Assignees
Labels
enhancement New feature or request

Comments

@ocadoret
Copy link
Contributor

ocadoret commented Nov 18, 2024

SharedUnionFieldsDeep doesn't work with Union that have undefined.

For example :

type Cat = {
  type: 'cat';
  name: string;
  catType: string;
};

type Dog = {
  type: 'dog';
  name: string;
  dogType: string;
};

type Pet = SharedUnionFieldsDeep<Cat | Dog>;
// type Pet = {
//   type: "cat" | "dog";
//   name: string;
// }

type OptionalPet = SharedUnionFieldsDeep<Cat | Dog | undefined>;
// type OptionalPet = Cat | Dog | undefined

Here, OptionalPet keeps the full union, but we might expect the following output :

type OptionalPet = SharedUnionFieldsDeep<Cat | Dog | undefined>;
// type Pet = {
//   type: "cat" | "dog";
//   name: string;
// } | undefined

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • The funding will be given to active contributors.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@ocadoret ocadoret added the enhancement New feature or request label Nov 18, 2024
@fregante
Copy link
Collaborator

I think it's reasonable to handle invalid types yourself before passing them to a function. I'd think Parameters<SomeFunc | undefined> also fails the same way for example.

@Emiyaaaaa
Copy link
Collaborator

I don't think this is a problem, it makes sense to merge public fields only if the original types is consistent, like if they are both an object.

@ocadoret
Copy link
Contributor Author

I see your point. I thought that it might be a good addition with an option like recurseIntoArrays (maybe allowUndefinedInUnion ?).
Especially with complex and deep types :

type A = {
  a: number;
  b: string;
  c: { d: string; e: number } | { d: string; f: string } | undefined;
}

With this example, it would be quite powerful to have something like this :

type SharedUnionA = SharedUnionFieldsDeep<A, {allowUndefinedInUnion: true}>;
// type SharedUnionA = {
//   a: number;
//   b: string;
//   c: { d: string } | undefined;
// }

Which is correct since as soon as c is defined, then d is string.

@Emiyaaaaa Emiyaaaaa self-assigned this Nov 23, 2024
@Emiyaaaaa Emiyaaaaa linked a pull request Nov 24, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants