Skip to content

Fix getSwitchClauseTypeOfWitnesses#3947

Merged
ahejlsberg merged 2 commits into
mainfrom
fix-3909
May 18, 2026
Merged

Fix getSwitchClauseTypeOfWitnesses#3947
ahejlsberg merged 2 commits into
mainfrom
fix-3909

Conversation

@ahejlsberg
Copy link
Copy Markdown
Member

Simpler fix than #3934.

Fixes #3909.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.ts with .types, .symbols, and .errors.txt baselines confirming x narrows to string and 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.

@ahejlsberg ahejlsberg added this pull request to the merge queue May 18, 2026
Merged via the queue into main with commit 9f17e24 May 18, 2026
25 checks passed
@ahejlsberg ahejlsberg deleted the fix-3909 branch May 18, 2026 15:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tsgo loses all type narrowing in a switch (typeof x) when any clause is case ""

3 participants