@@ -5620,7 +5620,7 @@ module ts {
5620
5620
}
5621
5621
5622
5622
var isConstEnum = isConstEnumObjectType(objectType);
5623
- if (isConstEnum &&
5623
+ if (isConstEnum &&
5624
5624
(!node.argumentExpression || node.argumentExpression.kind !== SyntaxKind.StringLiteral)) {
5625
5625
error(node.argumentExpression, Diagnostics.A_const_enum_member_can_only_be_accessed_using_a_string_literal);
5626
5626
return unknownType;
@@ -5652,10 +5652,10 @@ module ts {
5652
5652
}
5653
5653
5654
5654
// Check for compatible indexer types.
5655
- if (indexType.flags & ( TypeFlags.Any | TypeFlags.StringLike | TypeFlags.NumberLike)) {
5655
+ if (isTypeOfKind( indexType, TypeFlags.Any | TypeFlags.StringLike | TypeFlags.NumberLike)) {
5656
5656
5657
5657
// Try to use a number indexer.
5658
- if (indexType.flags & ( TypeFlags.Any | TypeFlags.NumberLike)) {
5658
+ if (isTypeOfKind( indexType, TypeFlags.Any | TypeFlags.NumberLike)) {
5659
5659
var numberIndexType = getIndexTypeOfType(objectType, IndexKind.Number);
5660
5660
if (numberIndexType) {
5661
5661
return numberIndexType;
@@ -6556,7 +6556,7 @@ module ts {
6556
6556
}
6557
6557
6558
6558
function checkArithmeticOperandType(operand: Node, type: Type, diagnostic: DiagnosticMessage): boolean {
6559
- if (!(type.flags & ( TypeFlags.Any | TypeFlags.NumberLike) )) {
6559
+ if (!isTypeOfKind (type, TypeFlags.Any | TypeFlags.NumberLike)) {
6560
6560
error(operand, diagnostic);
6561
6561
return false;
6562
6562
}
@@ -6707,12 +6707,21 @@ module ts {
6707
6707
return numberType;
6708
6708
}
6709
6709
6710
- // Return true if type an object type, a type parameter, or a union type composed of only those kinds of types
6711
- function isStructuredType(type: Type): boolean {
6710
+ // Return true if type has the given flags, or is a union type composed of types that all have those flags
6711
+ function isTypeOfKind(type: Type, kind: TypeFlags): boolean {
6712
+ if (type.flags & kind) {
6713
+ return true;
6714
+ }
6712
6715
if (type.flags & TypeFlags.Union) {
6713
- return !forEach((<UnionType>type).types, t => !isStructuredType(t));
6716
+ var types = (<UnionType>type).types;
6717
+ for (var i = 0; i < types.length; i++) {
6718
+ if (!(types[i].flags & kind)) {
6719
+ return false;
6720
+ }
6721
+ }
6722
+ return true;
6714
6723
}
6715
- return (type.flags & (TypeFlags.ObjectType | TypeFlags.TypeParameter)) !== 0 ;
6724
+ return false ;
6716
6725
}
6717
6726
6718
6727
function isConstEnumObjectType(type: Type): boolean {
@@ -6729,7 +6738,7 @@ module ts {
6729
6738
// and the right operand to be of type Any or a subtype of the 'Function' interface type.
6730
6739
// The result is always of the Boolean primitive type.
6731
6740
// NOTE: do not raise error if leftType is unknown as related error was already reported
6732
- if (!(leftType.flags & TypeFlags.Any || isStructuredType(leftType) )) {
6741
+ if (!isTypeOfKind (leftType, TypeFlags.Any | TypeFlags.ObjectType | TypeFlags.TypeParameter )) {
6733
6742
error(node.left, Diagnostics.The_left_hand_side_of_an_instanceof_expression_must_be_of_type_any_an_object_type_or_a_type_parameter);
6734
6743
}
6735
6744
// NOTE: do not raise error if right is unknown as related error was already reported
@@ -6744,10 +6753,10 @@ module ts {
6744
6753
// The in operator requires the left operand to be of type Any, the String primitive type, or the Number primitive type,
6745
6754
// and the right operand to be of type Any, an object type, or a type parameter type.
6746
6755
// The result is always of the Boolean primitive type.
6747
- if (leftType !== anyType && leftType !== stringType && leftType !== numberType ) {
6756
+ if (!isTypeOfKind( leftType, TypeFlags.Any | TypeFlags.StringLike | TypeFlags.NumberLike) ) {
6748
6757
error(node.left, Diagnostics.The_left_hand_side_of_an_in_expression_must_be_of_types_any_string_or_number);
6749
6758
}
6750
- if (!(rightType.flags & TypeFlags.Any || isStructuredType(rightType) )) {
6759
+ if (!isTypeOfKind (rightType, TypeFlags.Any | TypeFlags.ObjectType | TypeFlags.TypeParameter )) {
6751
6760
error(node.right, Diagnostics.The_right_hand_side_of_an_in_expression_must_be_of_type_any_an_object_type_or_a_type_parameter);
6752
6761
}
6753
6762
return booleanType;
@@ -6908,16 +6917,16 @@ module ts {
6908
6917
if (rightType.flags & (TypeFlags.Undefined | TypeFlags.Null)) rightType = leftType;
6909
6918
6910
6919
var resultType: Type;
6911
- if (leftType.flags & TypeFlags.NumberLike && rightType.flags & TypeFlags.NumberLike) {
6920
+ if (isTypeOfKind( leftType, TypeFlags.NumberLike) && isTypeOfKind( rightType, TypeFlags.NumberLike) ) {
6912
6921
// Operands of an enum type are treated as having the primitive type Number.
6913
6922
// If both operands are of the Number primitive type, the result is of the Number primitive type.
6914
6923
resultType = numberType;
6915
6924
}
6916
- else if (leftType.flags & TypeFlags.StringLike || rightType.flags & TypeFlags.StringLike) {
6925
+ else if (isTypeOfKind( leftType, TypeFlags.StringLike) || isTypeOfKind( rightType, TypeFlags.StringLike) ) {
6917
6926
// If one or both operands are of the String primitive type, the result is of the String primitive type.
6918
6927
resultType = stringType;
6919
6928
}
6920
- else if (leftType.flags & TypeFlags.Any || leftType === unknownType || rightType.flags & TypeFlags.Any || rightType === unknownType ) {
6929
+ else if (leftType.flags & TypeFlags.Any || rightType.flags & TypeFlags.Any) {
6921
6930
// Otherwise, the result is of type Any.
6922
6931
// NOTE: unknown type here denotes error type. Old compiler treated this case as any type so do we.
6923
6932
resultType = anyType;
@@ -8273,7 +8282,7 @@ module ts {
8273
8282
var exprType = checkExpression(node.expression);
8274
8283
// unknownType is returned i.e. if node.expression is identifier whose name cannot be resolved
8275
8284
// in this case error about missing name is already reported - do not report extra one
8276
- if (!(exprType.flags & TypeFlags.Any || isStructuredType(exprType) )) {
8285
+ if (!isTypeOfKind (exprType, TypeFlags.Any | TypeFlags.ObjectType | TypeFlags.TypeParameter )) {
8277
8286
error(node.expression, Diagnostics.The_right_hand_side_of_a_for_in_statement_must_be_of_type_any_an_object_type_or_a_type_parameter);
8278
8287
}
8279
8288
0 commit comments