Fix narrowing of destructured discriminated union parameters with defaults#63505
Open
wolfe8105 wants to merge 1 commit into
Open
Fix narrowing of destructured discriminated union parameters with defaults#63505wolfe8105 wants to merge 1 commit into
wolfe8105 wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds a compiler regression test for destructuring defaults in discriminated unions (issue #50139) and updates checker logic so narrowing works when binding elements/parameters have default initializers.
Changes:
- Add a new compiler test covering: (1) repro with defaults, (2) control without defaults, (3) soundness error case.
- Update
checker.tscontrol-flow/narrowing logic to consider binding elements/parameters even when they have default initializers. - Add new baseline outputs (
.types,.symbols,.js,.errors.txt) for the test.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| tests/cases/compiler/discriminatedUnionDestructuringWithDefaults.ts | New regression test for narrowing behavior with destructuring defaults in discriminated unions |
| tests/baselines/reference/discriminatedUnionDestructuringWithDefaults.types | Baseline types output for the new test |
| tests/baselines/reference/discriminatedUnionDestructuringWithDefaults.symbols | Baseline symbols output for the new test |
| tests/baselines/reference/discriminatedUnionDestructuringWithDefaults.js | Baseline JS emit output for the new test |
| tests/baselines/reference/discriminatedUnionDestructuringWithDefaults.errors.txt | Baseline expected error output for the soundness case |
| src/compiler/checker.ts | Adjust narrowing/reference logic to no longer exclude binding elements/parameters with initializers |
Comments suppressed due to low confidence (1)
src/compiler/checker.ts:1
- This condition used to explicitly exclude binding elements with an initializer; removing that guard is subtle and easy to accidentally revert later. Consider adding a brief comment here explaining that binding element default initializers are intentionally included so discriminated-union narrowing works for destructured bindings with defaults (as covered by the new test).
Author
|
@microsoft-github-policy-service agree |
Contributor
|
@wolfe8105 You should read this: #62963 |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #50139 — first PR here, happy to adjust style/scope as needed.
src/compiler/checker.tshad two predicates that skipped narrowing for destructured bindings with defaults:getCandidateDiscriminantPropertyAccessgetNarrowedTypeOfSymbolBoth guarded with
!declaration.initializer. Removed that clause from both. Narrowing now works through defaulted bindings.Declaration-time assignability of the default is checked by a separate pipeline, so foreign-type defaults still error at the binding.
Regression test added.
npx hereby runtestsshows no existing baselines change.Backlogmilestone (Discriminated union parameter destructuring doesn't work if the fields have defaults #50139)mainbranchhereby runtestslocally