Skip to content

Commit 7b1c496

Browse files
committed
Add IndexType to keep around 'indexType.typeOfIndex' in case we're using an enum as the index.
Add new global Number.Subtype Accept new test results but some quirks left to fix.
1 parent dc6b8fe commit 7b1c496

13 files changed

+297
-180
lines changed

src/compiler/checker.ts

Lines changed: 226 additions & 121 deletions
Large diffs are not rendered by default.

src/compiler/types.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,13 +1447,15 @@ module ts {
14471447
/* @internal */
14481448
ContainsObjectLiteral = 0x00080000, // Type is or contains object literal type
14491449
ESSymbol = 0x00100000, // Type of symbol primitive introduced in ES6
1450+
Subset = 0x00200000, // Type that has a subset of valid values
14501451

14511452
/* @internal */
14521453
Intrinsic = Any | String | Number | Boolean | ESSymbol | Void | Undefined | Null,
14531454
/* @internal */
14541455
Primitive = String | Number | Boolean | ESSymbol | Void | Undefined | Null | StringLiteral | Enum,
14551456
StringLike = String | StringLiteral,
14561457
NumberLike = Number | Enum,
1458+
SubsetMaybe = Enum | Reference,
14571459
ObjectType = Class | Interface | Reference | Tuple | Anonymous,
14581460
/* @internal */
14591461
RequiresWidening = ContainsUndefinedOrNull | ContainsObjectLiteral
@@ -1487,8 +1489,8 @@ module ts {
14871489
declaredProperties: Symbol[]; // Declared members
14881490
declaredCallSignatures: Signature[]; // Declared call signatures
14891491
declaredConstructSignatures: Signature[]; // Declared construct signatures
1490-
declaredStringIndexType: Type; // Declared string index type
1491-
declaredNumberIndexType: Type; // Declared numeric index type
1492+
declaredStringIndex: IndexType; // Declared string type
1493+
declaredNumberIndex: IndexType; // Declared numeric type
14921494
}
14931495

14941496
// Type references (TypeFlags.Reference)
@@ -1514,15 +1516,21 @@ module ts {
15141516
resolvedProperties: SymbolTable; // Cache of resolved properties
15151517
}
15161518

1517-
/* @internal */
1518-
// Resolved object or union type
1519+
export interface IndexType {
1520+
typeOfIndex?: Type // string|number|enum
1521+
typeOfValue: Type
1522+
declaredNode?: Declaration
1523+
inherited?: boolean
1524+
}
1525+
1526+
/* @internal */ // Resolved object or union type
15191527
export interface ResolvedType extends ObjectType, UnionType {
15201528
members: SymbolTable; // Properties by name
15211529
properties: Symbol[]; // Properties
15221530
callSignatures: Signature[]; // Call signatures of type
15231531
constructSignatures: Signature[]; // Construct signatures of type
1524-
stringIndexType: Type; // String index type
1525-
numberIndexType: Type; // Numeric index type
1532+
stringIndex: IndexType; // String index type
1533+
numberIndex: IndexType; // Number index type
15261534
}
15271535

15281536
// Type parameters (TypeFlags.TypeParameter)

tests/baselines/reference/enumAssignmentCompat.errors.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ tests/cases/compiler/enumAssignmentCompat.ts(26,5): error TS2322: Type 'typeof W
22
tests/cases/compiler/enumAssignmentCompat.ts(28,5): error TS2322: Type 'W' is not assignable to type 'typeof W'.
33
Property 'D' is missing in type 'Number'.
44
tests/cases/compiler/enumAssignmentCompat.ts(30,5): error TS2322: Type 'number' is not assignable to type 'typeof W'.
5+
Property 'D' is missing in type 'Number'.
56
tests/cases/compiler/enumAssignmentCompat.ts(32,5): error TS2322: Type 'W' is not assignable to type 'WStatic'.
67
Property 'a' is missing in type 'Number'.
78
tests/cases/compiler/enumAssignmentCompat.ts(33,5): error TS2322: Type 'number' is not assignable to type 'WStatic'.
9+
Property 'a' is missing in type 'Number'.
810

911

1012
==== tests/cases/compiler/enumAssignmentCompat.ts (5 errors) ====
@@ -45,6 +47,7 @@ tests/cases/compiler/enumAssignmentCompat.ts(33,5): error TS2322: Type 'number'
4547
var d: typeof W = 3; // error
4648
~
4749
!!! error TS2322: Type 'number' is not assignable to type 'typeof W'.
50+
!!! error TS2322: Property 'D' is missing in type 'Number'.
4851
var e: typeof W.a = 4;
4952
var f: WStatic = W.a; // error
5053
~
@@ -53,6 +56,7 @@ tests/cases/compiler/enumAssignmentCompat.ts(33,5): error TS2322: Type 'number'
5356
var g: WStatic = 5; // error
5457
~
5558
!!! error TS2322: Type 'number' is not assignable to type 'WStatic'.
59+
!!! error TS2322: Property 'a' is missing in type 'Number'.
5660
var h: W = 3;
5761
var i: W = W.a;
5862
i = W.a;

tests/baselines/reference/enumAssignmentCompat2.errors.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ tests/cases/compiler/enumAssignmentCompat2.ts(25,5): error TS2322: Type 'typeof
22
tests/cases/compiler/enumAssignmentCompat2.ts(27,5): error TS2322: Type 'W' is not assignable to type 'typeof W'.
33
Property 'a' is missing in type 'Number'.
44
tests/cases/compiler/enumAssignmentCompat2.ts(29,5): error TS2322: Type 'number' is not assignable to type 'typeof W'.
5+
Property 'a' is missing in type 'Number'.
56
tests/cases/compiler/enumAssignmentCompat2.ts(31,5): error TS2322: Type 'W' is not assignable to type 'WStatic'.
67
Property 'a' is missing in type 'Number'.
78
tests/cases/compiler/enumAssignmentCompat2.ts(32,5): error TS2322: Type 'number' is not assignable to type 'WStatic'.
9+
Property 'a' is missing in type 'Number'.
810

911

1012
==== tests/cases/compiler/enumAssignmentCompat2.ts (5 errors) ====
@@ -44,6 +46,7 @@ tests/cases/compiler/enumAssignmentCompat2.ts(32,5): error TS2322: Type 'number'
4446
var d: typeof W = 3; // error
4547
~
4648
!!! error TS2322: Type 'number' is not assignable to type 'typeof W'.
49+
!!! error TS2322: Property 'a' is missing in type 'Number'.
4750
var e: typeof W.a = 4;
4851
var f: WStatic = W.a; // error
4952
~
@@ -52,6 +55,7 @@ tests/cases/compiler/enumAssignmentCompat2.ts(32,5): error TS2322: Type 'number'
5255
var g: WStatic = 5; // error
5356
~
5457
!!! error TS2322: Type 'number' is not assignable to type 'WStatic'.
58+
!!! error TS2322: Property 'a' is missing in type 'Number'.
5559
var h: W = 3;
5660
var i: W = W.a;
5761
i = W.a;

tests/baselines/reference/genericCallWithObjectTypeArgsAndIndexersErrors.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithObjectTypeArgsAndIndexersErrors.ts(15,17): error TS2313: Constraint of a type parameter cannot reference any type parameter from the same type parameter list.
2-
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithObjectTypeArgsAndIndexersErrors.ts(18,9): error TS2413: Numeric index type 'T' is not assignable to string index type 'Object'.
2+
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithObjectTypeArgsAndIndexersErrors.ts(18,9): error TS2413: Numeric index type '[string]: T' is not assignable to string index type '[string]: Object'.
33
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithObjectTypeArgsAndIndexersErrors.ts(23,9): error TS2322: Type 'T' is not assignable to type 'U'.
44

55

@@ -25,7 +25,7 @@ tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithObj
2525
[x: string]: Object;
2626
[x: number]: T;
2727
~~~~~~~~~~~~~~~
28-
!!! error TS2413: Numeric index type 'T' is not assignable to string index type 'Object'.
28+
!!! error TS2413: Numeric index type '[string]: T' is not assignable to string index type '[string]: Object'.
2929
};
3030
var r2 = foo(b);
3131
var d = r2[1];

tests/baselines/reference/indexSignatureTypeCheck.errors.txt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ tests/cases/compiler/indexSignatureTypeCheck.ts(50,5): error TS2375: Duplicate n
77
tests/cases/compiler/indexSignatureTypeCheck.ts(55,5): error TS2375: Duplicate number index signature.
88
tests/cases/compiler/indexSignatureTypeCheck.ts(65,1): error TS2322: Type '{ [x: number]: string; }' is not assignable to type '{ [x: string]: string; }'.
99
Index signature is missing in type '{ [x: number]: string; }'.
10-
tests/cases/compiler/indexSignatureTypeCheck.ts(66,1): error TS2322: Type '{ [x: number]: number; }' is not assignable to type '{ [x: string]: string; }'.
11-
Index signature is missing in type '{ [x: number]: number; }'.
12-
tests/cases/compiler/indexSignatureTypeCheck.ts(70,1): error TS2322: Type '{ [x: number]: number; }' is not assignable to type '{ [x: number]: string; }'.
10+
tests/cases/compiler/indexSignatureTypeCheck.ts(66,1): error TS2322: Type '{ [x: E<number>]: number; }' is not assignable to type '{ [x: string]: string; }'.
11+
Index signature is missing in type '{ [x: E<number>]: number; }'.
12+
tests/cases/compiler/indexSignatureTypeCheck.ts(70,1): error TS2322: Type '{ [x: E<number>]: number; }' is not assignable to type '{ [x: number]: string; }'.
1313
Index signatures are incompatible.
1414
Type 'number' is not assignable to type 'string'.
15-
tests/cases/compiler/indexSignatureTypeCheck.ts(72,1): error TS2322: Type '{ [x: string]: string; }' is not assignable to type '{ [x: number]: number; }'.
15+
tests/cases/compiler/indexSignatureTypeCheck.ts(72,1): error TS2322: Type '{ [x: string]: string; }' is not assignable to type '{ [x: E<number>]: number; }'.
1616
Index signatures are incompatible.
1717
Type 'string' is not assignable to type 'number'.
18-
tests/cases/compiler/indexSignatureTypeCheck.ts(73,1): error TS2322: Type '{ [x: number]: string; }' is not assignable to type '{ [x: number]: number; }'.
18+
tests/cases/compiler/indexSignatureTypeCheck.ts(73,1): error TS2322: Type '{ [x: number]: string; }' is not assignable to type '{ [x: E<number>]: number; }'.
1919
Index signatures are incompatible.
2020
Type 'string' is not assignable to type 'number'.
2121

@@ -105,25 +105,25 @@ tests/cases/compiler/indexSignatureTypeCheck.ts(73,1): error TS2322: Type '{ [x:
105105
!!! error TS2322: Index signature is missing in type '{ [x: number]: string; }'.
106106
x = z;
107107
~
108-
!!! error TS2322: Type '{ [x: number]: number; }' is not assignable to type '{ [x: string]: string; }'.
109-
!!! error TS2322: Index signature is missing in type '{ [x: number]: number; }'.
108+
!!! error TS2322: Type '{ [x: E<number>]: number; }' is not assignable to type '{ [x: string]: string; }'.
109+
!!! error TS2322: Index signature is missing in type '{ [x: E<number>]: number; }'.
110110

111111
y = x;
112112
y = y;
113113
y = z;
114114
~
115-
!!! error TS2322: Type '{ [x: number]: number; }' is not assignable to type '{ [x: number]: string; }'.
115+
!!! error TS2322: Type '{ [x: E<number>]: number; }' is not assignable to type '{ [x: number]: string; }'.
116116
!!! error TS2322: Index signatures are incompatible.
117117
!!! error TS2322: Type 'number' is not assignable to type 'string'.
118118

119119
z = x;
120120
~
121-
!!! error TS2322: Type '{ [x: string]: string; }' is not assignable to type '{ [x: number]: number; }'.
121+
!!! error TS2322: Type '{ [x: string]: string; }' is not assignable to type '{ [x: E<number>]: number; }'.
122122
!!! error TS2322: Index signatures are incompatible.
123123
!!! error TS2322: Type 'string' is not assignable to type 'number'.
124124
z = y;
125125
~
126-
!!! error TS2322: Type '{ [x: number]: string; }' is not assignable to type '{ [x: number]: number; }'.
126+
!!! error TS2322: Type '{ [x: number]: string; }' is not assignable to type '{ [x: E<number>]: number; }'.
127127
!!! error TS2322: Index signatures are incompatible.
128128
!!! error TS2322: Type 'string' is not assignable to type 'number'.
129129
z = z;

tests/baselines/reference/indexTypeCheck.errors.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
tests/cases/compiler/indexTypeCheck.ts(2,2): error TS1021: An index signature must have a type annotation.
22
tests/cases/compiler/indexTypeCheck.ts(3,2): error TS1021: An index signature must have a type annotation.
3-
tests/cases/compiler/indexTypeCheck.ts(17,2): error TS2413: Numeric index type 'number' is not assignable to string index type 'string'.
4-
tests/cases/compiler/indexTypeCheck.ts(22,2): error TS2413: Numeric index type 'Orange' is not assignable to string index type 'Yellow'.
5-
tests/cases/compiler/indexTypeCheck.ts(27,2): error TS2413: Numeric index type 'number' is not assignable to string index type 'string'.
3+
tests/cases/compiler/indexTypeCheck.ts(17,2): error TS2413: Numeric index type '[string]: number' is not assignable to string index type '[string]: string'.
4+
tests/cases/compiler/indexTypeCheck.ts(22,2): error TS2413: Numeric index type '[string]: Orange' is not assignable to string index type '[string]: Yellow'.
5+
tests/cases/compiler/indexTypeCheck.ts(27,2): error TS2413: Numeric index type '[string]: number' is not assignable to string index type '[string]: string'.
66
tests/cases/compiler/indexTypeCheck.ts(32,3): error TS1096: An index signature must have exactly one parameter.
77
tests/cases/compiler/indexTypeCheck.ts(36,3): error TS1023: An index signature parameter type must be 'string', 'number', or an enum type.
88
tests/cases/compiler/indexTypeCheck.ts(51,1): error TS2342: An index expression argument must be of type 'string', 'number', 'symbol, or 'any'.
@@ -31,21 +31,21 @@ tests/cases/compiler/indexTypeCheck.ts(51,1): error TS2342: An index expression
3131
interface Orange {
3232
[n:number]: number; // ok
3333
~~~~~~~~~~~~~~~~~~~
34-
!!! error TS2413: Numeric index type 'number' is not assignable to string index type 'string'.
34+
!!! error TS2413: Numeric index type '[string]: number' is not assignable to string index type '[string]: string'.
3535
[s:string]: string; // error
3636
}
3737

3838
interface Green {
3939
[n:number]: Orange; // error
4040
~~~~~~~~~~~~~~~~~~~
41-
!!! error TS2413: Numeric index type 'Orange' is not assignable to string index type 'Yellow'.
41+
!!! error TS2413: Numeric index type '[string]: Orange' is not assignable to string index type '[string]: Yellow'.
4242
[s:string]: Yellow; // ok
4343
}
4444

4545
interface Cyan {
4646
[n:number]: number; // error
4747
~~~~~~~~~~~~~~~~~~~
48-
!!! error TS2413: Numeric index type 'number' is not assignable to string index type 'string'.
48+
!!! error TS2413: Numeric index type '[string]: number' is not assignable to string index type '[string]: string'.
4949
[s:string]: string; // ok
5050
}
5151

tests/baselines/reference/indexerConstraints.errors.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
tests/cases/compiler/indexerConstraints.ts(17,5): error TS2413: Numeric index type 'A' is not assignable to string index type 'B'.
2-
tests/cases/compiler/indexerConstraints.ts(25,5): error TS2413: Numeric index type 'A' is not assignable to string index type 'B'.
3-
tests/cases/compiler/indexerConstraints.ts(33,5): error TS2413: Numeric index type 'A' is not assignable to string index type 'B'.
4-
tests/cases/compiler/indexerConstraints.ts(41,5): error TS2413: Numeric index type 'A' is not assignable to string index type 'B'.
1+
tests/cases/compiler/indexerConstraints.ts(17,5): error TS2413: Numeric index type '[string]: A' is not assignable to string index type '[string]: B'.
2+
tests/cases/compiler/indexerConstraints.ts(25,5): error TS2413: Numeric index type '[string]: A' is not assignable to string index type '[string]: B'.
3+
tests/cases/compiler/indexerConstraints.ts(33,5): error TS2413: Numeric index type '[string]: A' is not assignable to string index type '[string]: B'.
4+
tests/cases/compiler/indexerConstraints.ts(41,5): error TS2413: Numeric index type '[string]: A' is not assignable to string index type '[string]: B'.
55

66

77
==== tests/cases/compiler/indexerConstraints.ts (4 errors) ====
@@ -23,7 +23,7 @@ tests/cases/compiler/indexerConstraints.ts(41,5): error TS2413: Numeric index ty
2323
interface E {
2424
[n: number]: A;
2525
~~~~~~~~~~~~~~~
26-
!!! error TS2413: Numeric index type 'A' is not assignable to string index type 'B'.
26+
!!! error TS2413: Numeric index type '[string]: A' is not assignable to string index type '[string]: B'.
2727
}
2828

2929
// Inheritance
@@ -33,7 +33,7 @@ tests/cases/compiler/indexerConstraints.ts(41,5): error TS2413: Numeric index ty
3333
interface G extends F {
3434
[n: number]: A;
3535
~~~~~~~~~~~~~~~
36-
!!! error TS2413: Numeric index type 'A' is not assignable to string index type 'B'.
36+
!!! error TS2413: Numeric index type '[string]: A' is not assignable to string index type '[string]: B'.
3737
}
3838

3939
// Other way
@@ -43,7 +43,7 @@ tests/cases/compiler/indexerConstraints.ts(41,5): error TS2413: Numeric index ty
4343
interface I extends H {
4444
[s: string]: B;
4545
~~~~~~~~~~~~~~~
46-
!!! error TS2413: Numeric index type 'A' is not assignable to string index type 'B'.
46+
!!! error TS2413: Numeric index type '[string]: A' is not assignable to string index type '[string]: B'.
4747
}
4848

4949
// With hidden indexer
@@ -53,6 +53,6 @@ tests/cases/compiler/indexerConstraints.ts(41,5): error TS2413: Numeric index ty
5353
interface K extends J {
5454
[n: number]: A;
5555
~~~~~~~~~~~~~~~
56-
!!! error TS2413: Numeric index type 'A' is not assignable to string index type 'B'.
56+
!!! error TS2413: Numeric index type '[string]: A' is not assignable to string index type '[string]: B'.
5757
[s: string]: B;
5858
}

tests/baselines/reference/indexerConstraints2.errors.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
tests/cases/compiler/indexerConstraints2.ts(9,5): error TS2413: Numeric index type 'A' is not assignable to string index type 'B'.
2-
tests/cases/compiler/indexerConstraints2.ts(17,5): error TS2413: Numeric index type 'A' is not assignable to string index type 'B'.
3-
tests/cases/compiler/indexerConstraints2.ts(26,5): error TS2413: Numeric index type 'A' is not assignable to string index type 'B'.
1+
tests/cases/compiler/indexerConstraints2.ts(9,5): error TS2413: Numeric index type '[string]: A' is not assignable to string index type '[string]: B'.
2+
tests/cases/compiler/indexerConstraints2.ts(17,5): error TS2413: Numeric index type '[string]: A' is not assignable to string index type '[string]: B'.
3+
tests/cases/compiler/indexerConstraints2.ts(26,5): error TS2413: Numeric index type '[string]: A' is not assignable to string index type '[string]: B'.
44

55

66
==== tests/cases/compiler/indexerConstraints2.ts (3 errors) ====
@@ -14,7 +14,7 @@ tests/cases/compiler/indexerConstraints2.ts(26,5): error TS2413: Numeric index t
1414
class G extends F {
1515
[n: number]: A
1616
~~~~~~~~~~~~~~
17-
!!! error TS2413: Numeric index type 'A' is not assignable to string index type 'B'.
17+
!!! error TS2413: Numeric index type '[string]: A' is not assignable to string index type '[string]: B'.
1818
}
1919

2020
// Other way
@@ -24,7 +24,7 @@ tests/cases/compiler/indexerConstraints2.ts(26,5): error TS2413: Numeric index t
2424
class I extends H {
2525
[s: string]: B
2626
~~~~~~~~~~~~~~
27-
!!! error TS2413: Numeric index type 'A' is not assignable to string index type 'B'.
27+
!!! error TS2413: Numeric index type '[string]: A' is not assignable to string index type '[string]: B'.
2828
}
2929

3030
// With hidden indexer
@@ -35,6 +35,6 @@ tests/cases/compiler/indexerConstraints2.ts(26,5): error TS2413: Numeric index t
3535
class K extends J {
3636
[n: number]: A;
3737
~~~~~~~~~~~~~~~
38-
!!! error TS2413: Numeric index type 'A' is not assignable to string index type 'B'.
38+
!!! error TS2413: Numeric index type '[string]: A' is not assignable to string index type '[string]: B'.
3939
[s: string]: B;
4040
}

tests/baselines/reference/inheritedStringIndexersFromDifferentBaseTypes2.errors.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
tests/cases/compiler/inheritedStringIndexersFromDifferentBaseTypes2.ts(18,11): error TS2413: Numeric index type '{}' is not assignable to string index type '{ a: any; }'.
1+
tests/cases/compiler/inheritedStringIndexersFromDifferentBaseTypes2.ts(3,5): error TS2413: Numeric index type '[string]: {}' is not assignable to string index type '[string]: { a: any; }'.
22

33

44
==== tests/cases/compiler/inheritedStringIndexersFromDifferentBaseTypes2.ts (1 errors) ====
55
// indexer in B is a subtype of indexer in A
66
interface A {
77
[s: string]: {
8+
~~~~~~~~~~~~~~
89
a;
10+
~~~~~~~~~~
911
};
12+
~~~~~~
13+
!!! error TS2413: Numeric index type '[string]: {}' is not assignable to string index type '[string]: { a: any; }'.
1014
}
1115
interface B {
1216
[s: number]: {
@@ -20,8 +24,6 @@ tests/cases/compiler/inheritedStringIndexersFromDifferentBaseTypes2.ts(18,11): e
2024
[s: number]: {};
2125
}
2226
interface E extends A, D { } // error
23-
~
24-
!!! error TS2413: Numeric index type '{}' is not assignable to string index type '{ a: any; }'.
2527

2628
interface F extends A, D {
2729
[s: number]: {

0 commit comments

Comments
 (0)