-
Notifications
You must be signed in to change notification settings - Fork 15.7k
Fix non-null discriminant narrowing consistency #64257
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
Open
S.H Jeong (z0rimo)
wants to merge
17
commits into
microsoft:main
Choose a base branch
from
z0rimo:fix/discriminated-union-nullability-62511
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+128
−4
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
bc24da7
fix: narrow discriminant access after non-null assertion
z0rimo 857b696
fix: preserve nullability for aliased discriminants
z0rimo b57d7fd
fix: preserve optional-chain nullability
z0rimo e4900c6
test: cover mirrored non-null discriminant access
z0rimo 11434b3
fix: limit eager narrowing to side-effect-free expressions
z0rimo f6291ea
fix: restrict eager narrowing to direct strict comparisons
z0rimo 0b51865
fix: exclude computed keys from eager narrowing
z0rimo deb68a2
test: cover nullable optimized narrowing guards
z0rimo 8812a74
fix: recognize signed primitive discriminants
z0rimo d2b05ca
Simplify non-null discriminant narrowing tests
z0rimo 9be1c7a
Merge branch 'main' into fix/discriminated-union-nullability-62511
z0rimo 592ccc7
fix: align nullable discriminant narrowing paths
z0rimo e6ea6ed
Merge branch 'main' into fix/discriminated-union-nullability-62511
z0rimo 80d227b
Merge branch 'main' into fix/discriminated-union-nullability-62511
z0rimo 0b930b7
Merge branch 'main' into fix/discriminated-union-nullability-62511
z0rimo c7c7f2f
Merge branch 'main' into fix/discriminated-union-nullability-62511
z0rimo 25b6377
Fix nullable discriminant fast-path consistency
z0rimo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
tsc/testdata/tests/cases/compiler/discriminatedUnionNonNullAccessNarrowing.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| // @strict: true | ||
| // @noEmit: true | ||
| // @noTypesAndSymbols: true | ||
|
|
||
| // A non-null assertion narrows only the asserted expression itself; it does not establish a | ||
| // persistent non-null flow fact for the underlying reference. This must hold identically for | ||
| // unions small enough to use the fallback discriminant narrowing path and unions large enough | ||
| // to use the optimized key-property path, so that a `!` assertion behaves consistently | ||
| // regardless of union size (#62511). Optional chaining remains the idiomatic way to narrow the | ||
| // reference itself and continues to do so unchanged. | ||
|
|
||
| type Small = | ||
| | { type: "1" } | ||
| | { type: "2" } | ||
| | undefined; | ||
|
|
||
| type Large = | ||
| | { type: "1" } | ||
| | { type: "2" } | ||
| | { type: "3" } | ||
| | { type: "4" } | ||
| | { type: "5" } | ||
| | { type: "6" } | ||
| | { type: "7" } | ||
| | { type: "8" } | ||
| | { type: "9" } | ||
| | { type: "10" } | ||
| | undefined; | ||
|
|
||
| type LargeWithNull = Exclude<Large, undefined> | null; | ||
|
|
||
| // Strict equality, both branches. | ||
|
|
||
| declare let smallEqual: Small; | ||
| if (smallEqual!.type === "1") { | ||
| // @ts-expect-error | ||
| smallEqual.type; | ||
| } else { | ||
| // @ts-expect-error | ||
| smallEqual.type; | ||
| } | ||
|
|
||
| declare let largeEqual: Large; | ||
| if (largeEqual!.type === "1") { | ||
| // @ts-expect-error | ||
| largeEqual.type; | ||
| } else { | ||
| // @ts-expect-error | ||
| largeEqual.type; | ||
| } | ||
|
|
||
| // Strict inequality. | ||
|
|
||
| declare let smallNotEqual: Small; | ||
| if (smallNotEqual!.type !== "1") { | ||
| // @ts-expect-error | ||
| smallNotEqual.type; | ||
| } | ||
|
|
||
| declare let largeNotEqual: Large; | ||
| if (largeNotEqual!.type !== "1") { | ||
| // @ts-expect-error | ||
| largeNotEqual.type; | ||
| } | ||
|
|
||
| // Switch. | ||
|
|
||
| declare let smallSwitch: Small; | ||
| switch (smallSwitch!.type) { | ||
| case "1": | ||
| // @ts-expect-error | ||
| smallSwitch.type; | ||
| break; | ||
| } | ||
|
|
||
| declare let largeSwitch: Large; | ||
| switch (largeSwitch!.type) { | ||
| case "1": | ||
| // @ts-expect-error | ||
| largeSwitch.type; | ||
| break; | ||
| } | ||
|
|
||
| // `null` constituents are preserved just like `undefined`. | ||
|
|
||
| declare let largeNullEqual: LargeWithNull; | ||
| if (largeNullEqual!.type === "1") { | ||
| // @ts-expect-error | ||
| largeNullEqual.type; | ||
| } | ||
|
|
||
| declare let largeNullSwitch: LargeWithNull; | ||
| switch (largeNullSwitch!.type) { | ||
| case "1": | ||
| // @ts-expect-error | ||
| largeNullSwitch.type; | ||
| break; | ||
| } | ||
|
|
||
| // Control: optional chaining is unaffected and remains the idiomatic way to narrow the | ||
| // reference itself, for both union sizes. | ||
|
|
||
| declare let smallOptional: Small; | ||
| if (smallOptional?.type === "1") { | ||
| smallOptional.type; | ||
| } else { | ||
| // @ts-expect-error | ||
| smallOptional.type; | ||
| } | ||
|
|
||
| declare let largeOptional: Large; | ||
| if (largeOptional?.type === "1") { | ||
| largeOptional.type; | ||
| } else { | ||
| // @ts-expect-error | ||
| largeOptional.type; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.