Skip to content

Commit 5fef080

Browse files
committed
Merge pull request #9003 from Microsoft/caseUndefinedAndNull
Allow case comparison to undefined and null in strict null checking mode
2 parents 849ab7c + b4f1214 commit 5fef080

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15110,7 +15110,7 @@ namespace ts {
1511015110
// In a 'switch' statement, each 'case' expression must be of a type that is comparable
1511115111
// to or from the type of the 'switch' expression.
1511215112
const caseType = checkExpression(caseClause.expression);
15113-
if (!isTypeComparableTo(expressionType, caseType)) {
15113+
if (!isTypeEqualityComparableTo(expressionType, caseType)) {
1511415114
// expressionType is not comparable to caseType, try the reversed check and report errors if it fails
1511515115
checkTypeComparableTo(caseType, expressionType, caseClause.expression, /*headMessage*/ undefined);
1511615116
}

tests/baselines/reference/equalityStrictNulls.errors.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,14 @@ tests/cases/conformance/types/typeRelationships/comparable/equalityStrictNulls.t
8181
!!! error TS2365: Operator '<=' cannot be applied to types 'number' and 'undefined'.
8282
}
8383
}
84+
function f5(x: string) {
85+
switch(x) {
86+
case null:
87+
break;
88+
case undefined:
89+
break;
90+
default:
91+
return;
92+
}
93+
}
8494

tests/baselines/reference/equalityStrictNulls.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ function f4(x: number) {
6767
if (x <= undefined) {
6868
}
6969
}
70+
function f5(x: string) {
71+
switch(x) {
72+
case null:
73+
break;
74+
case undefined:
75+
break;
76+
default:
77+
return;
78+
}
79+
}
7080

7181

7282
//// [equalityStrictNulls.js]
@@ -134,3 +144,13 @@ function f4(x) {
134144
if (x <= undefined) {
135145
}
136146
}
147+
function f5(x) {
148+
switch (x) {
149+
case null:
150+
break;
151+
case undefined:
152+
break;
153+
default:
154+
return;
155+
}
156+
}

tests/cases/conformance/types/typeRelationships/comparable/equalityStrictNulls.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,13 @@ function f4(x: number) {
6767
if (x <= undefined) {
6868
}
6969
}
70+
function f5(x: string) {
71+
switch(x) {
72+
case null:
73+
break;
74+
case undefined:
75+
break;
76+
default:
77+
return;
78+
}
79+
}

0 commit comments

Comments
 (0)