Skip to content

Allow case comparison to undefined and null in strict null checking mode #9003

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 7, 2016
Merged
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
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15106,7 +15106,7 @@ namespace ts {
// In a 'switch' statement, each 'case' expression must be of a type that is comparable
// to or from the type of the 'switch' expression.
const caseType = checkExpression(caseClause.expression);
if (!isTypeComparableTo(expressionType, caseType)) {
if (!isTypeEqualityComparableTo(expressionType, caseType)) {
// expressionType is not comparable to caseType, try the reversed check and report errors if it fails
checkTypeComparableTo(caseType, expressionType, caseClause.expression, /*headMessage*/ undefined);
}
Expand Down
10 changes: 10 additions & 0 deletions tests/baselines/reference/equalityStrictNulls.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,14 @@ tests/cases/conformance/types/typeRelationships/comparable/equalityStrictNulls.t
!!! error TS2365: Operator '<=' cannot be applied to types 'number' and 'undefined'.
}
}
function f5(x: string) {
switch(x) {
case null:
break;
case undefined:
break;
default:
return;
}
}

20 changes: 20 additions & 0 deletions tests/baselines/reference/equalityStrictNulls.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ function f4(x: number) {
if (x <= undefined) {
}
}
function f5(x: string) {
switch(x) {
case null:
break;
case undefined:
break;
default:
return;
}
}


//// [equalityStrictNulls.js]
Expand Down Expand Up @@ -134,3 +144,13 @@ function f4(x) {
if (x <= undefined) {
}
}
function f5(x) {
switch (x) {
case null:
break;
case undefined:
break;
default:
return;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,13 @@ function f4(x: number) {
if (x <= undefined) {
}
}
function f5(x: string) {
switch(x) {
case null:
break;
case undefined:
break;
default:
return;
}
}