Fix getSwitchClauseTypeOfWitnesses#3947
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a bug in getSwitchClauseTypeOfWitnesses where a case "" clause inside a switch (typeof x) aborted witness collection entirely, disabling all type narrowing for the switch. The previous code conflated "expression is not a string literal" with "expression is the empty string literal" since both produced text == "". The fix separates these two concerns: a non-string-literal expression aborts witness collection (as before), while an empty string literal is now treated as an empty witness slot, allowing narrowing to proceed via the other clauses.
Changes:
- Split the
text == ""early-out into a non-string-literal check that nils witnesses, and a separate empty/deduplicated witness handling for string literal text. - Added a compiler regression test
typeofSwitchEmptyStringCase.tswith.types,.symbols, and.errors.txtbaselines confirmingxnarrows tostringand only the expected TS2678 error is reported.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| internal/checker/flow.go | Separates non-string-literal abort from empty-string handling in switch typeof witness collection. |
| testdata/tests/cases/compiler/typeofSwitchEmptyStringCase.ts | New regression test for issue #3909. |
| testdata/baselines/reference/compiler/typeofSwitchEmptyStringCase.types | Baseline showing x correctly narrowed to string. |
| testdata/baselines/reference/compiler/typeofSwitchEmptyStringCase.symbols | Symbol baseline for the new test. |
| testdata/baselines/reference/compiler/typeofSwitchEmptyStringCase.errors.txt | Error baseline showing only the expected TS2678 diagnostic. |
jakebailey
approved these changes
May 18, 2026
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.
Simpler fix than #3934.
Fixes #3909.