How to Use Logical AND (&&) in a TypeScript Switch Case Statement
In TypeScript, a logical AND (&&) expression is valid inside a case clause because the compiler evaluates it as a single expression and compares the result to the switch value using strict equality (===).
The TypeScript compiler processes switch statements by evaluating the switch expression once and comparing it against each case clause using the strict equality algorithm. According to the microsoft/TypeScript source code, the parser handles logical AND operators within case expressions through the CaseClause AST node, while the type checker validates compatibility between expression types.
How TypeScript Processes Case Expressions
In the TypeScript compiler architecture, a switch statement evaluates its expression once, and each case clause contains a single expression compared to that value using strict equality (===).
According to the source code in src/compiler/parser.ts, the parseCaseClause function (lines 7012–7031) creates a CaseClause node whose expression property holds the parsed expression. The scanner recognizes the && token as AmpersandAmpersandToken in src/compiler/scanner.ts (line 262), allowing the parser to handle logical AND operations within case expressions. The AST visitor in src/compiler/visitorPublic.ts (lines 1713–1714) walks these nodes during transformation.
Type Checking Logical AND in Cases
The TypeScript type checker validates that the case expression's type is compatible with the type of the switch expression. In src/compiler/checker.ts, the getTypeOfSwitchClause function (lines 28451–28477) performs this validation.
When you write case (a && b):, the compiler treats this as a normal expression: it evaluates a && b, yields either a (if falsy) or b (if a is truthy), and then performs the === comparison against the switch value. The CaseClause interface is defined in src/compiler/types.ts (lines 3481–3497), which specifies that the expression property can hold any valid expression, including logical operations.
Recommended Patterns for Logical Conditions
Direct Logical AND Comparison
Use this pattern when the switch value itself should match the result of an && expression. This is rare but valid:
switch (value) {
case (cond1 && cond2):
// Matches when value === (cond1 && cond2)
break;
}
Switch on True (Conditional Switch)
The most common pattern for evaluating arbitrary boolean conditions uses switch (true). This forces each case expression to be compared against the boolean true:
switch (true) {
case cond1 && cond2:
// Executes when both conditions are truthy
break;
case cond3:
// Executes when cond3 is truthy
break;
}
Stacked Cases for Multiple Values
When logic requires handling multiple discrete values without logical operators, stack the cases:
switch (value) {
case 'a':
case 'b':
// Handles both 'a' and 'b'
break;
}
Complete Code Examples
Using && for Direct Comparison
const flag = true;
const value = 42;
switch (value) {
case (flag && 42): // Evaluates to 42 → matches
console.log('matched via &&');
break;
default:
console.log('no match');
}
The expression flag && 42 evaluates to 42 because flag is truthy. The comparison value === 42 succeeds.
Conditional Switch with Multiple Conditions
function handle(input: string) {
const isNumber = /^\d+$/.test(input);
const isHex = /^0x[0-9a-f]+$/i.test(input);
switch (true) {
case isNumber && isHex: // Both true → hex number like "0xFF"
console.log('hex number');
break;
case isNumber && !isHex: // Numeric but not hex
console.log('decimal number');
break;
case input === 'quit':
console.log('quit command');
break;
default:
console.log('unrecognized');
}
}
Each case evaluates a boolean expression; the first truthy match executes.
Fallback with Stacked Cases
const day = 'Saturday';
switch (day) {
case 'Saturday':
case 'Sunday':
console.log('weekend');
break;
default:
console.log('weekday');
}
Summary
- TypeScript
caseclauses accept any single expression, including logical AND (&&) operations, as implemented insrc/compiler/parser.ts(lines 7012–7031). - The compiler evaluates the expression and compares it to the switch value using strict equality (
===), verified bygetTypeOfSwitchClauseinsrc/compiler/checker.ts(lines 28451–28477). - The
switch (true)pattern provides the most readable approach for testing multiple boolean conditions with logical operators. - For multiple discrete values, prefer stacked
caselabels over logical operators for clarity and type safety.
Frequently Asked Questions
Can you use logical operators in TypeScript switch cases?
Yes. The TypeScript parser explicitly supports logical operators like && and || within case expressions. In src/compiler/scanner.ts (line 262), the && token is recognized as AmpersandAmpersandToken, and the parser handles it as part of the expression in parseCaseClause (lines 7012–7031). The expression evaluates to a single value that undergoes strict equality comparison.
Why does the switch (true) pattern work with logical AND?
The switch (true) pattern works because each case expression is evaluated and compared to true using strict equality. When you write case (cond1 && cond2):, the logical AND returns either a falsy value or the second operand. If both conditions are truthy, the result equals true, causing the case to match. This pattern leverages the CaseClause AST node defined in src/compiler/types.ts (lines 3481–3497).
What is the difference between case (a && b) and case a: case b:?
case (a && b) evaluates the logical AND as an expression and compares the result to the switch value, matching only when switchValue === (a && b). In contrast, case a: followed by case b: creates fall-through behavior where the code executes if the switch value equals a OR equals b. The latter handles discrete values, while the former evaluates truthiness and returns the last evaluated operand.
Is there a performance impact when using && in switch cases?
No. The TypeScript compiler produces JavaScript that evaluates the expression at runtime using standard ECMAScript semantics. The presence of && in a case expression does not trigger additional compiler overhead beyond the normal expression parsing handled by parseCaseClause in src/compiler/parser.ts and type checking in getTypeOfSwitchClause within src/compiler/checker.ts.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →