mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-16 07:13:45 -05:00
Merge pull request #5639 from Microsoft/typecheckStatementsInCaseClause
always check statements in case clause
This commit is contained in:
@@ -12886,13 +12886,13 @@ namespace ts {
|
||||
// In a 'switch' statement, each 'case' expression must be of a type that is assignable to or from the type of the 'switch' expression.
|
||||
const caseType = checkExpression(caseClause.expression);
|
||||
|
||||
// Permit 'number[] | "foo"' to be asserted to 'string'.
|
||||
if (expressionTypeIsStringLike && someConstituentTypeHasKind(caseType, TypeFlags.StringLike)) {
|
||||
return;
|
||||
}
|
||||
const expressionTypeIsAssignableToCaseType =
|
||||
// Permit 'number[] | "foo"' to be asserted to 'string'.
|
||||
(expressionTypeIsStringLike && someConstituentTypeHasKind(caseType, TypeFlags.StringLike)) ||
|
||||
isTypeAssignableTo(expressionType, caseType);
|
||||
|
||||
if (!isTypeAssignableTo(expressionType, caseType)) {
|
||||
// check 'expressionType isAssignableTo caseType' failed, try the reversed check and report errors if it fails
|
||||
if (!expressionTypeIsAssignableToCaseType) {
|
||||
// 'expressionType is not assignable to caseType', try the reversed check and report errors if it fails
|
||||
checkTypeAssignableTo(caseType, expressionType, caseClause.expression, /*headMessage*/ undefined);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user