Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1718,7 +1718,8 @@ function createBinder(): (file: SourceFile, options: CompilerOptions) => void {
preSwitchCaseFlow = currentFlow;
bind(node.caseBlock);
addAntecedent(postSwitchLabel, currentFlow);
const hasDefault = forEach(node.caseBlock.clauses, c => c.kind === SyntaxKind.DefaultClause);
const hasDefault = forEach(node.caseBlock.clauses, c => c.kind === SyntaxKind.DefaultClause) ||
(skipParentheses(node.expression).kind === SyntaxKind.TrueKeyword && node.caseBlock.clauses.some(clause => clause.kind === SyntaxKind.CaseClause && skipParentheses(clause.expression).kind === SyntaxKind.TrueKeyword));
// We mark a switch statement as possibly exhaustive if it has no default clause and if all
// case clauses have unreachable end points (e.g. they all return). Note, we no longer need
// this property in control flow analysis, it's there only for backwards compatibility.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//// [tests/cases/compiler/narrowByClauseExpressionInSwitchTrue11.ts] ////

=== narrowByClauseExpressionInSwitchTrue11.ts ===
function uhoh(input: string) : "A"|"B"|"C" {
>uhoh : Symbol(uhoh, Decl(narrowByClauseExpressionInSwitchTrue11.ts, 0, 0))
>input : Symbol(input, Decl(narrowByClauseExpressionInSwitchTrue11.ts, 0, 14))

switch (true) {
case /a/.test(input):
>/a/.test : Symbol(RegExp.test, Decl(lib.es5.d.ts, --, --))
>test : Symbol(RegExp.test, Decl(lib.es5.d.ts, --, --))
>input : Symbol(input, Decl(narrowByClauseExpressionInSwitchTrue11.ts, 0, 14))

return "A";
case /b/.test(input):
>/b/.test : Symbol(RegExp.test, Decl(lib.es5.d.ts, --, --))
>test : Symbol(RegExp.test, Decl(lib.es5.d.ts, --, --))
>input : Symbol(input, Decl(narrowByClauseExpressionInSwitchTrue11.ts, 0, 14))

return "B";
case true:
return "C";
}
}

function uhoh1(input: string) : "A"|"B"|"C" {
>uhoh1 : Symbol(uhoh1, Decl(narrowByClauseExpressionInSwitchTrue11.ts, 9, 1))
>input : Symbol(input, Decl(narrowByClauseExpressionInSwitchTrue11.ts, 11, 15))

switch ((true)) {
case /a/.test(input):
>/a/.test : Symbol(RegExp.test, Decl(lib.es5.d.ts, --, --))
>test : Symbol(RegExp.test, Decl(lib.es5.d.ts, --, --))
>input : Symbol(input, Decl(narrowByClauseExpressionInSwitchTrue11.ts, 11, 15))

return "A";
case /b/.test(input):
>/b/.test : Symbol(RegExp.test, Decl(lib.es5.d.ts, --, --))
>test : Symbol(RegExp.test, Decl(lib.es5.d.ts, --, --))
>input : Symbol(input, Decl(narrowByClauseExpressionInSwitchTrue11.ts, 11, 15))

return "B";
case (true):
return "C";
}
}
110 changes: 110 additions & 0 deletions tests/baselines/reference/narrowByClauseExpressionInSwitchTrue11.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
//// [tests/cases/compiler/narrowByClauseExpressionInSwitchTrue11.ts] ////

=== narrowByClauseExpressionInSwitchTrue11.ts ===
function uhoh(input: string) : "A"|"B"|"C" {
>uhoh : (input: string) => "A" | "B" | "C"
> : ^ ^^ ^^^^^
>input : string
> : ^^^^^^

switch (true) {
>true : true
> : ^^^^

case /a/.test(input):
>/a/.test(input) : boolean
> : ^^^^^^^
>/a/.test : (string: string) => boolean
> : ^ ^^ ^^^^^
>/a/ : RegExp
> : ^^^^^^
>test : (string: string) => boolean
> : ^ ^^ ^^^^^
>input : string
> : ^^^^^^

return "A";
>"A" : "A"
> : ^^^

case /b/.test(input):
>/b/.test(input) : boolean
> : ^^^^^^^
>/b/.test : (string: string) => boolean
> : ^ ^^ ^^^^^
>/b/ : RegExp
> : ^^^^^^
>test : (string: string) => boolean
> : ^ ^^ ^^^^^
>input : string
> : ^^^^^^

return "B";
>"B" : "B"
> : ^^^

case true:
>true : true
> : ^^^^

return "C";
>"C" : "C"
> : ^^^
}
}

function uhoh1(input: string) : "A"|"B"|"C" {
>uhoh1 : (input: string) => "A" | "B" | "C"
> : ^ ^^ ^^^^^
>input : string
> : ^^^^^^

switch ((true)) {
>(true) : true
> : ^^^^
>true : true
> : ^^^^

case /a/.test(input):
>/a/.test(input) : boolean
> : ^^^^^^^
>/a/.test : (string: string) => boolean
> : ^ ^^ ^^^^^
>/a/ : RegExp
> : ^^^^^^
>test : (string: string) => boolean
> : ^ ^^ ^^^^^
>input : string
> : ^^^^^^

return "A";
>"A" : "A"
> : ^^^

case /b/.test(input):
>/b/.test(input) : boolean
> : ^^^^^^^
>/b/.test : (string: string) => boolean
> : ^ ^^ ^^^^^
>/b/ : RegExp
> : ^^^^^^
>test : (string: string) => boolean
> : ^ ^^ ^^^^^
>input : string
> : ^^^^^^

return "B";
>"B" : "B"
> : ^^^

case (true):
>(true) : true
> : ^^^^
>true : true
> : ^^^^

return "C";
>"C" : "C"
> : ^^^
}
}
24 changes: 24 additions & 0 deletions tests/cases/compiler/narrowByClauseExpressionInSwitchTrue11.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// @strict: true
// @noEmit: true

function uhoh(input: string) : "A"|"B"|"C" {
switch (true) {
Copy link
Member

Choose a reason for hiding this comment

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

Awful, but, what about switch (false) + case false:?

Copy link
Contributor Author

@Zzzen Zzzen Jul 18, 2025

Choose a reason for hiding this comment

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

To my surprise, it seems some folks are writing switch(false). No case false: found though. Guess it wouldn't hurt to support it too! https://github.com/search?q=switch%28false%29%7B+language%3Atypescript&type=code&p=1

Copy link
Contributor Author

Choose a reason for hiding this comment

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

However, after some research, I don’t think we want to support it since switch(false) isn’t something we generally support.

playground

type A = { type: "A" };
type B = { type: "B" };
type AorB = A | B;

const isA = (x: AorB): x is A => x.type === "A";
const isB = (x: AorB): x is B => x.type === "B";

function test1(x: AorB) {
  switch (false) {
    case isA(x):
      // in a perfect world, x would have type B
      x;
      break;
    case isB(x):
      // in a perfect world, x would have type A
      x;
      break;
  }
}

function test2(x: AorB) {
  switch (true) {
    case isA(x):
      x;
      break;
    case isB(x):
      x;
      break;
  }
}

case /a/.test(input):
return "A";
case /b/.test(input):
return "B";
case true:
return "C";
}
}

function uhoh1(input: string) : "A"|"B"|"C" {
switch ((true)) {
case /a/.test(input):
return "A";
case /b/.test(input):
return "B";
case (true):
return "C";
}
}