-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Fix: Make discriminant property selection order-independent in unions (#62512) #62516
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
Conversation
|
@microsoft-github-policy-service agree |
|
Hi maintainers, All required jobs except 'test' have passed. The 'test' job was marked as "abandoned", and I noticed two matrix jobs ('Test Node 24 on macos-latest', 'Test Node 16 on macos-latest') were cancelled. All other checks (lint, coverage, typecheck, etc.) succeeded, and my change does not affect these environments specifically. Could you please help re-run the test jobs or advise if any further action is needed on my part? |
|
You didn't actually send any code, you only sent a broken test |
|
I have now committed the actual code change to checker.ts—sorry for the earlier omission! |
|
I am not sure we need to do this; the new ported compiler is consistent about this. |
|
If the new compiler is now consistent regarding discriminant property selection, I’m happy to close this PR. Would it be worthwhile to keep the order-independence test as a regression test to help catch any future regressions, or should I close the PR entirely? |
|
Just the test would be great actually. Agreed we don't need the code changes |
|
I've updated the PR to include only the test commit as suggested. Thanks for the review! |
|
Appreciated! |
Fix: Make discriminant property selection order-independent in unions
Summary
This PR fixes an issue where TypeScript's narrowing for discriminated unions depended on the order of union members. Previously, the compiler would select the first property with a unit (literal) type as the discriminant, which could lead to incorrect narrowing and inconsistent behavior based on type order.
Related Issue
Fixes #62512 (Type checking of discriminated nullable union depends on type order)
Approach
getKeyPropertyNamefunction to scan all union members and select the most common unit-typed property as the discriminant, rather than picking the first one found.Tests
discriminantOrderIndependence.tsto demonstrate and verify order-independence for discriminant selection.discriminantOrderIndependence.jsdiscriminantOrderIndependence.typesdiscriminantOrderIndependence.symbolsNotes
Thank you for reviewing!