mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-15 03:23:08 -06:00
Merge pull request #9003 from Microsoft/caseUndefinedAndNull
Allow case comparison to undefined and null in strict null checking mode
This commit is contained in:
commit
5fef0804c8
@ -15110,7 +15110,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);
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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]
|
||||
@ -134,3 +144,13 @@ function f4(x) {
|
||||
if (x <= undefined) {
|
||||
}
|
||||
}
|
||||
function f5(x) {
|
||||
switch (x) {
|
||||
case null:
|
||||
break;
|
||||
case undefined:
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user