Error when LHS of instanceof is Union of Primitives #18519 (#19063)

* Error when LHS of instanceof is Union of Primitives #18519

* Refactor to allTypesAssignableToKind method and update test

* Use ternary expression instead of if / else blocks
This commit is contained in:
Charles Pierce
2017-11-28 15:05:06 -08:00
committed by Mohamed Hegazy
parent 94ea38859b
commit 72da4b8f12
6 changed files with 121 additions and 1 deletions

View File

@@ -18314,6 +18314,12 @@ namespace ts {
(kind & TypeFlags.NonPrimitive && isTypeAssignableTo(source, nonPrimitiveType));
}
function allTypesAssignableToKind(source: Type, kind: TypeFlags, strict?: boolean): boolean {
return source.flags & TypeFlags.Union ?
every((source as UnionType).types, subType => allTypesAssignableToKind(subType, kind, strict)) :
isTypeAssignableToKind(source, kind, strict);
}
function isConstEnumObjectType(type: Type): boolean {
return getObjectFlags(type) & ObjectFlags.Anonymous && type.symbol && isConstEnumSymbol(type.symbol);
}
@@ -18331,7 +18337,8 @@ namespace ts {
// and the right operand to be of type Any, a subtype of the 'Function' interface type, or have a call or construct signature.
// The result is always of the Boolean primitive type.
// NOTE: do not raise error if leftType is unknown as related error was already reported
if (!isTypeAny(leftType) && isTypeAssignableToKind(leftType, TypeFlags.Primitive)) {
if (!isTypeAny(leftType) &&
allTypesAssignableToKind(leftType, TypeFlags.Primitive)) {
error(left, Diagnostics.The_left_hand_side_of_an_instanceof_expression_must_be_of_type_any_an_object_type_or_a_type_parameter);
}
// NOTE: do not raise error if right is unknown as related error was already reported