mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-06 11:54:44 -06:00
Added specific checks for comparing stringlike types.
This commit is contained in:
parent
5e2314303f
commit
8dbfe1ca63
@ -9400,7 +9400,12 @@ namespace ts {
|
||||
let targetType = getTypeFromTypeNode(node.type);
|
||||
if (produceDiagnostics && targetType !== unknownType) {
|
||||
let widenedType = getWidenedType(exprType);
|
||||
if (!(isTypeAssignableTo(targetType, widenedType))) {
|
||||
|
||||
// Permit 'number[] | "foo"' to be asserted to 'string'.
|
||||
const bothAreStringLike =
|
||||
someConstituentTypeHasKind(targetType, TypeFlags.StringLike) &&
|
||||
someConstituentTypeHasKind(widenedType, TypeFlags.StringLike);
|
||||
if (!bothAreStringLike && !(isTypeAssignableTo(targetType, widenedType))) {
|
||||
checkTypeAssignableTo(exprType, targetType, node, Diagnostics.Neither_type_0_nor_type_1_is_assignable_to_the_other);
|
||||
}
|
||||
}
|
||||
@ -10244,6 +10249,10 @@ namespace ts {
|
||||
case SyntaxKind.ExclamationEqualsToken:
|
||||
case SyntaxKind.EqualsEqualsEqualsToken:
|
||||
case SyntaxKind.ExclamationEqualsEqualsToken:
|
||||
// Permit 'number[] | "foo"' to be asserted to 'string'.
|
||||
if (someConstituentTypeHasKind(leftType, TypeFlags.StringLike) && someConstituentTypeHasKind(rightType, TypeFlags.StringLike)) {
|
||||
return booleanType;
|
||||
}
|
||||
if (!isTypeAssignableTo(leftType, rightType) && !isTypeAssignableTo(rightType, leftType)) {
|
||||
reportOperatorError();
|
||||
}
|
||||
@ -12703,6 +12712,7 @@ namespace ts {
|
||||
let hasDuplicateDefaultClause = false;
|
||||
|
||||
let expressionType = checkExpression(node.expression);
|
||||
const expressionTypeIsStringLike = someConstituentTypeHasKind(expressionType, TypeFlags.StringLike);
|
||||
forEach(node.caseBlock.clauses, clause => {
|
||||
// Grammar check for duplicate default clauses, skip if we already report duplicate default clause
|
||||
if (clause.kind === SyntaxKind.DefaultClause && !hasDuplicateDefaultClause) {
|
||||
@ -12723,6 +12733,12 @@ namespace ts {
|
||||
// TypeScript 1.0 spec (April 2014):5.9
|
||||
// In a 'switch' statement, each 'case' expression must be of a type that is assignable to or from the type of the 'switch' expression.
|
||||
let caseType = checkExpression(caseClause.expression);
|
||||
|
||||
// Permit 'number[] | "foo"' to be asserted to 'string'.
|
||||
if (expressionTypeIsStringLike && someConstituentTypeHasKind(caseType, TypeFlags.StringLike)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isTypeAssignableTo(expressionType, caseType)) {
|
||||
// check 'expressionType isAssignableTo caseType' failed, try the reversed check and report errors if it fails
|
||||
checkTypeAssignableTo(caseType, expressionType, caseClause.expression, /*headMessage*/ undefined);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user