Skip to content

Commit 8573850

Browse files
committed
Consistent handling of string-like and number-like values in expressions
1 parent 1ce32d9 commit 8573850

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

src/compiler/checker.ts

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5621,7 +5621,7 @@ module ts {
56215621
}
56225622

56235623
var isConstEnum = isConstEnumObjectType(objectType);
5624-
if (isConstEnum &&
5624+
if (isConstEnum &&
56255625
(!node.argumentExpression || node.argumentExpression.kind !== SyntaxKind.StringLiteral)) {
56265626
error(node.argumentExpression, Diagnostics.A_const_enum_member_can_only_be_accessed_using_a_string_literal);
56275627
return unknownType;
@@ -5653,10 +5653,10 @@ module ts {
56535653
}
56545654

56555655
// Check for compatible indexer types.
5656-
if (indexType.flags & (TypeFlags.Any | TypeFlags.StringLike | TypeFlags.NumberLike) || isTypeAssignableTo(indexType, stringOrNumberType)) {
5656+
if (isTypeOfKind(indexType, TypeFlags.Any | TypeFlags.StringLike | TypeFlags.NumberLike)) {
56575657

56585658
// Try to use a number indexer.
5659-
if (indexType.flags & (TypeFlags.Any | TypeFlags.NumberLike) || isTypeAssignableTo(indexType, numberType)) {
5659+
if (isTypeOfKind(indexType, TypeFlags.Any | TypeFlags.NumberLike)) {
56605660
var numberIndexType = getIndexTypeOfType(objectType, IndexKind.Number);
56615661
if (numberIndexType) {
56625662
return numberIndexType;
@@ -6557,7 +6557,7 @@ module ts {
65576557
}
65586558

65596559
function checkArithmeticOperandType(operand: Node, type: Type, diagnostic: DiagnosticMessage): boolean {
6560-
if (!(type.flags & (TypeFlags.Any | TypeFlags.NumberLike))) {
6560+
if (!isTypeOfKind(type, TypeFlags.Any | TypeFlags.NumberLike)) {
65616561
error(operand, diagnostic);
65626562
return false;
65636563
}
@@ -6708,12 +6708,21 @@ module ts {
67086708
return numberType;
67096709
}
67106710

6711-
// Return true if type an object type, a type parameter, or a union type composed of only those kinds of types
6712-
function isStructuredType(type: Type): boolean {
6711+
// Return true if type has the given flags, or is a union type composed of types that all have those flags
6712+
function isTypeOfKind(type: Type, kind: TypeFlags): boolean {
6713+
if (type.flags & kind) {
6714+
return true;
6715+
}
67136716
if (type.flags & TypeFlags.Union) {
6714-
return !forEach((<UnionType>type).types, t => !isStructuredType(t));
6717+
var types = (<UnionType>type).types;
6718+
for (var i = 0; i < types.length; i++) {
6719+
if (!(types[i].flags & kind)) {
6720+
return false;
6721+
}
6722+
}
6723+
return true;
67156724
}
6716-
return (type.flags & (TypeFlags.ObjectType | TypeFlags.TypeParameter)) !== 0;
6725+
return false;
67176726
}
67186727

67196728
function isConstEnumObjectType(type: Type): boolean {
@@ -6730,7 +6739,7 @@ module ts {
67306739
// and the right operand to be of type Any or a subtype of the 'Function' interface type.
67316740
// The result is always of the Boolean primitive type.
67326741
// NOTE: do not raise error if leftType is unknown as related error was already reported
6733-
if (!(leftType.flags & TypeFlags.Any || isStructuredType(leftType))) {
6742+
if (!isTypeOfKind(leftType, TypeFlags.Any | TypeFlags.ObjectType | TypeFlags.TypeParameter)) {
67346743
error(node.left, Diagnostics.The_left_hand_side_of_an_instanceof_expression_must_be_of_type_any_an_object_type_or_a_type_parameter);
67356744
}
67366745
// NOTE: do not raise error if right is unknown as related error was already reported
@@ -6745,10 +6754,10 @@ module ts {
67456754
// The in operator requires the left operand to be of type Any, the String primitive type, or the Number primitive type,
67466755
// and the right operand to be of type Any, an object type, or a type parameter type.
67476756
// The result is always of the Boolean primitive type.
6748-
if (leftType !== anyType && leftType !== stringType && leftType !== numberType) {
6757+
if (!isTypeOfKind(leftType, TypeFlags.Any | TypeFlags.StringLike | TypeFlags.NumberLike)) {
67496758
error(node.left, Diagnostics.The_left_hand_side_of_an_in_expression_must_be_of_types_any_string_or_number);
67506759
}
6751-
if (!(rightType.flags & TypeFlags.Any || isStructuredType(rightType))) {
6760+
if (!isTypeOfKind(rightType, TypeFlags.Any | TypeFlags.ObjectType | TypeFlags.TypeParameter)) {
67526761
error(node.right, Diagnostics.The_right_hand_side_of_an_in_expression_must_be_of_type_any_an_object_type_or_a_type_parameter);
67536762
}
67546763
return booleanType;
@@ -6909,16 +6918,16 @@ module ts {
69096918
if (rightType.flags & (TypeFlags.Undefined | TypeFlags.Null)) rightType = leftType;
69106919

69116920
var resultType: Type;
6912-
if (leftType.flags & TypeFlags.NumberLike && rightType.flags & TypeFlags.NumberLike) {
6921+
if (isTypeOfKind(leftType, TypeFlags.NumberLike) && isTypeOfKind(rightType, TypeFlags.NumberLike)) {
69136922
// Operands of an enum type are treated as having the primitive type Number.
69146923
// If both operands are of the Number primitive type, the result is of the Number primitive type.
69156924
resultType = numberType;
69166925
}
6917-
else if (leftType.flags & TypeFlags.StringLike || rightType.flags & TypeFlags.StringLike) {
6926+
else if (isTypeOfKind(leftType, TypeFlags.StringLike) || isTypeOfKind(rightType, TypeFlags.StringLike)) {
69186927
// If one or both operands are of the String primitive type, the result is of the String primitive type.
69196928
resultType = stringType;
69206929
}
6921-
else if (leftType.flags & TypeFlags.Any || leftType === unknownType || rightType.flags & TypeFlags.Any || rightType === unknownType) {
6930+
else if (leftType.flags & TypeFlags.Any || rightType.flags & TypeFlags.Any) {
69226931
// Otherwise, the result is of type Any.
69236932
// NOTE: unknown type here denotes error type. Old compiler treated this case as any type so do we.
69246933
resultType = anyType;
@@ -8274,7 +8283,7 @@ module ts {
82748283
var exprType = checkExpression(node.expression);
82758284
// unknownType is returned i.e. if node.expression is identifier whose name cannot be resolved
82768285
// in this case error about missing name is already reported - do not report extra one
8277-
if (!(exprType.flags & TypeFlags.Any || isStructuredType(exprType))) {
8286+
if (!isTypeOfKind(exprType, TypeFlags.Any | TypeFlags.ObjectType | TypeFlags.TypeParameter)) {
82788287
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);
82798288
}
82808289

0 commit comments

Comments
 (0)