Skip to content

Commit 8a9b66d

Browse files
committed
fix hasExhaustiveCaseClauses for 'case never:'
1 parent 4825c2f commit 8a9b66d

File tree

3 files changed

+6
-2
lines changed

3 files changed

+6
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
**Bugfixes:**
1010

1111
* `getConstructorTypeOfClassLikeDeclaration` now really returns the constructor type (the static side of the class), previously it returned the instance type
12-
* `hasExhaustiveCaseClauses` allows additional case clauses with `null` and `undefined`
12+
* `hasExhaustiveCaseClauses` allows additional case clauses with `null`, `undefined` and `never`
1313

1414
# 3.18.0
1515

test/rules/exhaustive-switch/test.ts.lint

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ declare function get<T>(): T;
6565
case 1:
6666
case null:
6767
case undefined:
68+
case get<never>():
6869
return;
6970
}
7071
debugger;

util/util.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1710,7 +1710,10 @@ export function hasExhaustiveCaseClauses(node: ts.SwitchStatement, checker: ts.T
17101710
return false;
17111711
const seen = new Set<string | undefined>();
17121712
for (const clause of caseClauses) {
1713-
const type = getPrimitiveLiteralFromType(checker.getTypeAtLocation(clause.expression));
1713+
const expressionType = checker.getTypeAtLocation(clause.expression);
1714+
if (isTypeFlagSet(expressionType, ts.TypeFlags.Never))
1715+
continue; // additional case clause with 'never' is always allowed
1716+
const type = getPrimitiveLiteralFromType(expressionType);
17141717
if (types.has(type)) {
17151718
seen.add(type);
17161719
} else if (type !== 'null' && type !== 'undefined') { // additional case clauses with 'null' and 'undefined' are always allowed

0 commit comments

Comments
 (0)