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

C extends A but C does not extend A | B #59034

Open
jer-sen opened this issue Jun 26, 2024 · 3 comments
Open

C extends A but C does not extend A | B #59034

jer-sen opened this issue Jun 26, 2024 · 3 comments
Labels
Help Wanted You can do this Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Milestone

Comments

@jer-sen
Copy link

jer-sen commented Jun 26, 2024

πŸ”Ž Search Terms

union extends

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about unions and extends

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.5.2#code/C4TwDgpgBAglC8UDeUCGAuKBGANFARpgM7ABOAlgHYDmAvlAD7JqYBMUehUJFNtA3AFgAUKEhQAQgmYYorPAGNiZKtSgCRY6AGFpKWVkZzOy3mo3DN4aAHUA9qQDWRADwAVKBAAewCJQAmRLDS2gB80m5CotZQACJ2EEQAcnbA9k7unj5+gcFMUohhEfxAA

πŸ’» Code

type A = { a: 1, b: string} | { a: 2 , b: string};
type B = { a: 2, c: string };
type C = { a: 1 | 2, b: string };

type Works<T extends A = C> = T;
type DoesNotWork<T extends A | B = C> = T;

πŸ™ Actual behavior

An error tells that C does not extend A | B

πŸ™‚ Expected behavior

No error since C extends A and therefore extends A | B

Additional information about the issue

No response

@Andarist
Copy link
Contributor

This is a fallout of an extra attempt to match combinations in typeRelatedToDiscriminatedType. The normal union-based match fails here in both cases and it's just that fallback match that even allows Works to work.

I'd probably call it a design limitation as that fallback match is something that, IIRC, breaks some general rules. { prop: A } | { prop: B } isn't always interchangeable with { prop: A | B }. This fallback match also only works to a certain number of combinations so it's already not something that works universally.

@jer-sen
Copy link
Author

jer-sen commented Jun 26, 2024

Maybe the fallback match could be improved a bit. The repro is very simple and should be handle correctly IMHO.

@RyanCavanaugh RyanCavanaugh added Help Wanted You can do this Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases labels Jun 26, 2024
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Jun 26, 2024
@ethanresnick
Copy link
Contributor

Possibly related? #36969

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Help Wanted You can do this Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Projects
None yet
Development

No branches or pull requests

4 participants