Skip to content

Commit 436b0f6

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 4ff881c commit 436b0f6

14 files changed

+302
-184
lines changed

src/compiler/checker.ts

Lines changed: 225 additions & 120 deletions
Large diffs are not rendered by default.

src/compiler/types.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,11 +1423,13 @@ module ts {
14231423
ContainsUndefinedOrNull = 0x00040000, // Type is or contains Undefined or Null type
14241424
ContainsObjectLiteral = 0x00080000, // Type is or contains object literal type
14251425
ESSymbol = 0x00100000, // Type of symbol primitive introduced in ES6
1426+
Subset = 0x00200000, // Type that has a subset of valid values
14261427

14271428
Intrinsic = Any | String | Number | Boolean | ESSymbol | Void | Undefined | Null,
14281429
Primitive = String | Number | Boolean | ESSymbol | Void | Undefined | Null | StringLiteral | Enum,
14291430
StringLike = String | StringLiteral,
14301431
NumberLike = Number | Enum,
1432+
SubsetMaybe = Enum | Reference,
14311433
ObjectType = Class | Interface | Reference | Tuple | Anonymous,
14321434
RequiresWidening = ContainsUndefinedOrNull | ContainsObjectLiteral
14331435
}
@@ -1459,8 +1461,8 @@ module ts {
14591461
declaredProperties: Symbol[]; // Declared members
14601462
declaredCallSignatures: Signature[]; // Declared call signatures
14611463
declaredConstructSignatures: Signature[]; // Declared construct signatures
1462-
declaredStringIndexType: Type; // Declared string index type
1463-
declaredNumberIndexType: Type; // Declared numeric index type
1464+
declaredStringIndex: IndexType; // Declared string type
1465+
declaredNumberIndex: IndexType; // Declared numeric type
14641466
}
14651467

14661468
// Type references (TypeFlags.Reference)
@@ -1484,14 +1486,21 @@ module ts {
14841486
resolvedProperties: SymbolTable; // Cache of resolved properties
14851487
}
14861488

1489+
export interface IndexType {
1490+
typeOfIndex?: Type // string|number|enum
1491+
typeOfValue: Type
1492+
declaredNode?: Declaration
1493+
inherited?: boolean
1494+
}
1495+
14871496
// Resolved object or union type
14881497
export interface ResolvedType extends ObjectType, UnionType {
14891498
members: SymbolTable; // Properties by name
14901499
properties: Symbol[]; // Properties
14911500
callSignatures: Signature[]; // Call signatures of type
14921501
constructSignatures: Signature[]; // Construct signatures of type
1493-
stringIndexType: Type; // String index type
1494-
numberIndexType: Type; // Numeric index type
1502+
stringIndex: IndexType; // String index type
1503+
numberIndex: IndexType; // Number index type
14951504
}
14961505

14971506
// Type parameters (TypeFlags.TypeParameter)

tests/baselines/reference/decoratorOnClassMethod11.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
//// [decoratorOnClassMethod11.ts]
2-
module M {
3-
class C {
4-
decorator(target: Object, key: string): void { }
5-
6-
@this.decorator
7-
method() { }
8-
}
2+
module M {
3+
class C {
4+
decorator(target: Object, key: string): void { }
5+
6+
@this.decorator
7+
method() { }
8+
}
99
}
1010

1111
//// [decoratorOnClassMethod11.js]

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)