Skip to content

Commit

Permalink
Add IndexType to keep around 'indexType.typeOfIndex' in case we're us…
Browse files Browse the repository at this point in the history
…ing an enum as the index.

Add new global Number.Subtype
Accept new test results but some quirks left to fix.
  • Loading branch information
jbondc committed Apr 11, 2015
1 parent dc6b8fe commit 7b1c496
Show file tree
Hide file tree
Showing 13 changed files with 297 additions and 180 deletions.
347 changes: 226 additions & 121 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

20 changes: 14 additions & 6 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1447,13 +1447,15 @@ module ts {
/* @internal */
ContainsObjectLiteral = 0x00080000, // Type is or contains object literal type
ESSymbol = 0x00100000, // Type of symbol primitive introduced in ES6
Subset = 0x00200000, // Type that has a subset of valid values

/* @internal */
Intrinsic = Any | String | Number | Boolean | ESSymbol | Void | Undefined | Null,
/* @internal */
Primitive = String | Number | Boolean | ESSymbol | Void | Undefined | Null | StringLiteral | Enum,
StringLike = String | StringLiteral,
NumberLike = Number | Enum,
SubsetMaybe = Enum | Reference,
ObjectType = Class | Interface | Reference | Tuple | Anonymous,
/* @internal */
RequiresWidening = ContainsUndefinedOrNull | ContainsObjectLiteral
Expand Down Expand Up @@ -1487,8 +1489,8 @@ module ts {
declaredProperties: Symbol[]; // Declared members
declaredCallSignatures: Signature[]; // Declared call signatures
declaredConstructSignatures: Signature[]; // Declared construct signatures
declaredStringIndexType: Type; // Declared string index type
declaredNumberIndexType: Type; // Declared numeric index type
declaredStringIndex: IndexType; // Declared string type
declaredNumberIndex: IndexType; // Declared numeric type
}

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

/* @internal */
// Resolved object or union type
export interface IndexType {
typeOfIndex?: Type // string|number|enum
typeOfValue: Type
declaredNode?: Declaration
inherited?: boolean
}

/* @internal */ // Resolved object or union type
export interface ResolvedType extends ObjectType, UnionType {
members: SymbolTable; // Properties by name
properties: Symbol[]; // Properties
callSignatures: Signature[]; // Call signatures of type
constructSignatures: Signature[]; // Construct signatures of type
stringIndexType: Type; // String index type
numberIndexType: Type; // Numeric index type
stringIndex: IndexType; // String index type
numberIndex: IndexType; // Number index type
}

// Type parameters (TypeFlags.TypeParameter)
Expand Down
4 changes: 4 additions & 0 deletions tests/baselines/reference/enumAssignmentCompat.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ tests/cases/compiler/enumAssignmentCompat.ts(26,5): error TS2322: Type 'typeof W
tests/cases/compiler/enumAssignmentCompat.ts(28,5): error TS2322: Type 'W' is not assignable to type 'typeof W'.
Property 'D' is missing in type 'Number'.
tests/cases/compiler/enumAssignmentCompat.ts(30,5): error TS2322: Type 'number' is not assignable to type 'typeof W'.
Property 'D' is missing in type 'Number'.
tests/cases/compiler/enumAssignmentCompat.ts(32,5): error TS2322: Type 'W' is not assignable to type 'WStatic'.
Property 'a' is missing in type 'Number'.
tests/cases/compiler/enumAssignmentCompat.ts(33,5): error TS2322: Type 'number' is not assignable to type 'WStatic'.
Property 'a' is missing in type 'Number'.


==== tests/cases/compiler/enumAssignmentCompat.ts (5 errors) ====
Expand Down Expand Up @@ -45,6 +47,7 @@ tests/cases/compiler/enumAssignmentCompat.ts(33,5): error TS2322: Type 'number'
var d: typeof W = 3; // error
~
!!! error TS2322: Type 'number' is not assignable to type 'typeof W'.
!!! error TS2322: Property 'D' is missing in type 'Number'.
var e: typeof W.a = 4;
var f: WStatic = W.a; // error
~
Expand All @@ -53,6 +56,7 @@ tests/cases/compiler/enumAssignmentCompat.ts(33,5): error TS2322: Type 'number'
var g: WStatic = 5; // error
~
!!! error TS2322: Type 'number' is not assignable to type 'WStatic'.
!!! error TS2322: Property 'a' is missing in type 'Number'.
var h: W = 3;
var i: W = W.a;
i = W.a;
Expand Down
4 changes: 4 additions & 0 deletions tests/baselines/reference/enumAssignmentCompat2.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ tests/cases/compiler/enumAssignmentCompat2.ts(25,5): error TS2322: Type 'typeof
tests/cases/compiler/enumAssignmentCompat2.ts(27,5): error TS2322: Type 'W' is not assignable to type 'typeof W'.
Property 'a' is missing in type 'Number'.
tests/cases/compiler/enumAssignmentCompat2.ts(29,5): error TS2322: Type 'number' is not assignable to type 'typeof W'.
Property 'a' is missing in type 'Number'.
tests/cases/compiler/enumAssignmentCompat2.ts(31,5): error TS2322: Type 'W' is not assignable to type 'WStatic'.
Property 'a' is missing in type 'Number'.
tests/cases/compiler/enumAssignmentCompat2.ts(32,5): error TS2322: Type 'number' is not assignable to type 'WStatic'.
Property 'a' is missing in type 'Number'.


==== tests/cases/compiler/enumAssignmentCompat2.ts (5 errors) ====
Expand Down Expand Up @@ -44,6 +46,7 @@ tests/cases/compiler/enumAssignmentCompat2.ts(32,5): error TS2322: Type 'number'
var d: typeof W = 3; // error
~
!!! error TS2322: Type 'number' is not assignable to type 'typeof W'.
!!! error TS2322: Property 'a' is missing in type 'Number'.
var e: typeof W.a = 4;
var f: WStatic = W.a; // error
~
Expand All @@ -52,6 +55,7 @@ tests/cases/compiler/enumAssignmentCompat2.ts(32,5): error TS2322: Type 'number'
var g: WStatic = 5; // error
~
!!! error TS2322: Type 'number' is not assignable to type 'WStatic'.
!!! error TS2322: Property 'a' is missing in type 'Number'.
var h: W = 3;
var i: W = W.a;
i = W.a;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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.
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithObjectTypeArgsAndIndexersErrors.ts(18,9): error TS2413: Numeric index type 'T' is not assignable to string index type 'Object'.
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'.
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithObjectTypeArgsAndIndexersErrors.ts(23,9): error TS2322: Type 'T' is not assignable to type 'U'.


Expand All @@ -25,7 +25,7 @@ tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithObj
[x: string]: Object;
[x: number]: T;
~~~~~~~~~~~~~~~
!!! error TS2413: Numeric index type 'T' is not assignable to string index type 'Object'.
!!! error TS2413: Numeric index type '[string]: T' is not assignable to string index type '[string]: Object'.
};
var r2 = foo(b);
var d = r2[1];
Expand Down
20 changes: 10 additions & 10 deletions tests/baselines/reference/indexSignatureTypeCheck.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ tests/cases/compiler/indexSignatureTypeCheck.ts(50,5): error TS2375: Duplicate n
tests/cases/compiler/indexSignatureTypeCheck.ts(55,5): error TS2375: Duplicate number index signature.
tests/cases/compiler/indexSignatureTypeCheck.ts(65,1): error TS2322: Type '{ [x: number]: string; }' is not assignable to type '{ [x: string]: string; }'.
Index signature is missing in type '{ [x: number]: string; }'.
tests/cases/compiler/indexSignatureTypeCheck.ts(66,1): error TS2322: Type '{ [x: number]: number; }' is not assignable to type '{ [x: string]: string; }'.
Index signature is missing in type '{ [x: number]: number; }'.
tests/cases/compiler/indexSignatureTypeCheck.ts(70,1): error TS2322: Type '{ [x: number]: number; }' is not assignable to type '{ [x: number]: string; }'.
tests/cases/compiler/indexSignatureTypeCheck.ts(66,1): error TS2322: Type '{ [x: E<number>]: number; }' is not assignable to type '{ [x: string]: string; }'.
Index signature is missing in type '{ [x: E<number>]: number; }'.
tests/cases/compiler/indexSignatureTypeCheck.ts(70,1): error TS2322: Type '{ [x: E<number>]: number; }' is not assignable to type '{ [x: number]: string; }'.
Index signatures are incompatible.
Type 'number' is not assignable to type 'string'.
tests/cases/compiler/indexSignatureTypeCheck.ts(72,1): error TS2322: Type '{ [x: string]: string; }' is not assignable to type '{ [x: number]: number; }'.
tests/cases/compiler/indexSignatureTypeCheck.ts(72,1): error TS2322: Type '{ [x: string]: string; }' is not assignable to type '{ [x: E<number>]: number; }'.
Index signatures are incompatible.
Type 'string' is not assignable to type 'number'.
tests/cases/compiler/indexSignatureTypeCheck.ts(73,1): error TS2322: Type '{ [x: number]: string; }' is not assignable to type '{ [x: number]: number; }'.
tests/cases/compiler/indexSignatureTypeCheck.ts(73,1): error TS2322: Type '{ [x: number]: string; }' is not assignable to type '{ [x: E<number>]: number; }'.
Index signatures are incompatible.
Type 'string' is not assignable to type 'number'.

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

y = x;
y = y;
y = z;
~
!!! error TS2322: Type '{ [x: number]: number; }' is not assignable to type '{ [x: number]: string; }'.
!!! error TS2322: Type '{ [x: E<number>]: number; }' is not assignable to type '{ [x: number]: string; }'.
!!! error TS2322: Index signatures are incompatible.
!!! error TS2322: Type 'number' is not assignable to type 'string'.

z = x;
~
!!! error TS2322: Type '{ [x: string]: string; }' is not assignable to type '{ [x: number]: number; }'.
!!! error TS2322: Type '{ [x: string]: string; }' is not assignable to type '{ [x: E<number>]: number; }'.
!!! error TS2322: Index signatures are incompatible.
!!! error TS2322: Type 'string' is not assignable to type 'number'.
z = y;
~
!!! error TS2322: Type '{ [x: number]: string; }' is not assignable to type '{ [x: number]: number; }'.
!!! error TS2322: Type '{ [x: number]: string; }' is not assignable to type '{ [x: E<number>]: number; }'.
!!! error TS2322: Index signatures are incompatible.
!!! error TS2322: Type 'string' is not assignable to type 'number'.
z = z;
Expand Down
12 changes: 6 additions & 6 deletions tests/baselines/reference/indexTypeCheck.errors.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
tests/cases/compiler/indexTypeCheck.ts(2,2): error TS1021: An index signature must have a type annotation.
tests/cases/compiler/indexTypeCheck.ts(3,2): error TS1021: An index signature must have a type annotation.
tests/cases/compiler/indexTypeCheck.ts(17,2): error TS2413: Numeric index type 'number' is not assignable to string index type 'string'.
tests/cases/compiler/indexTypeCheck.ts(22,2): error TS2413: Numeric index type 'Orange' is not assignable to string index type 'Yellow'.
tests/cases/compiler/indexTypeCheck.ts(27,2): error TS2413: Numeric index type 'number' is not assignable to string index type 'string'.
tests/cases/compiler/indexTypeCheck.ts(17,2): error TS2413: Numeric index type '[string]: number' is not assignable to string index type '[string]: string'.
tests/cases/compiler/indexTypeCheck.ts(22,2): error TS2413: Numeric index type '[string]: Orange' is not assignable to string index type '[string]: Yellow'.
tests/cases/compiler/indexTypeCheck.ts(27,2): error TS2413: Numeric index type '[string]: number' is not assignable to string index type '[string]: string'.
tests/cases/compiler/indexTypeCheck.ts(32,3): error TS1096: An index signature must have exactly one parameter.
tests/cases/compiler/indexTypeCheck.ts(36,3): error TS1023: An index signature parameter type must be 'string', 'number', or an enum type.
tests/cases/compiler/indexTypeCheck.ts(51,1): error TS2342: An index expression argument must be of type 'string', 'number', 'symbol, or 'any'.
Expand Down Expand Up @@ -31,21 +31,21 @@ tests/cases/compiler/indexTypeCheck.ts(51,1): error TS2342: An index expression
interface Orange {
[n:number]: number; // ok
~~~~~~~~~~~~~~~~~~~
!!! error TS2413: Numeric index type 'number' is not assignable to string index type 'string'.
!!! error TS2413: Numeric index type '[string]: number' is not assignable to string index type '[string]: string'.
[s:string]: string; // error
}

interface Green {
[n:number]: Orange; // error
~~~~~~~~~~~~~~~~~~~
!!! error TS2413: Numeric index type 'Orange' is not assignable to string index type 'Yellow'.
!!! error TS2413: Numeric index type '[string]: Orange' is not assignable to string index type '[string]: Yellow'.
[s:string]: Yellow; // ok
}

interface Cyan {
[n:number]: number; // error
~~~~~~~~~~~~~~~~~~~
!!! error TS2413: Numeric index type 'number' is not assignable to string index type 'string'.
!!! error TS2413: Numeric index type '[string]: number' is not assignable to string index type '[string]: string'.
[s:string]: string; // ok
}

Expand Down
16 changes: 8 additions & 8 deletions tests/baselines/reference/indexerConstraints.errors.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
tests/cases/compiler/indexerConstraints.ts(17,5): error TS2413: Numeric index type 'A' is not assignable to string index type 'B'.
tests/cases/compiler/indexerConstraints.ts(25,5): error TS2413: Numeric index type 'A' is not assignable to string index type 'B'.
tests/cases/compiler/indexerConstraints.ts(33,5): error TS2413: Numeric index type 'A' is not assignable to string index type 'B'.
tests/cases/compiler/indexerConstraints.ts(41,5): error TS2413: Numeric index type 'A' is not assignable to string index type 'B'.
tests/cases/compiler/indexerConstraints.ts(17,5): error TS2413: Numeric index type '[string]: A' is not assignable to string index type '[string]: B'.
tests/cases/compiler/indexerConstraints.ts(25,5): error TS2413: Numeric index type '[string]: A' is not assignable to string index type '[string]: B'.
tests/cases/compiler/indexerConstraints.ts(33,5): error TS2413: Numeric index type '[string]: A' is not assignable to string index type '[string]: B'.
tests/cases/compiler/indexerConstraints.ts(41,5): error TS2413: Numeric index type '[string]: A' is not assignable to string index type '[string]: B'.


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

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

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

// With hidden indexer
Expand All @@ -53,6 +53,6 @@ tests/cases/compiler/indexerConstraints.ts(41,5): error TS2413: Numeric index ty
interface K extends J {
[n: number]: A;
~~~~~~~~~~~~~~~
!!! error TS2413: Numeric index type 'A' is not assignable to string index type 'B'.
!!! error TS2413: Numeric index type '[string]: A' is not assignable to string index type '[string]: B'.
[s: string]: B;
}
12 changes: 6 additions & 6 deletions tests/baselines/reference/indexerConstraints2.errors.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
tests/cases/compiler/indexerConstraints2.ts(9,5): error TS2413: Numeric index type 'A' is not assignable to string index type 'B'.
tests/cases/compiler/indexerConstraints2.ts(17,5): error TS2413: Numeric index type 'A' is not assignable to string index type 'B'.
tests/cases/compiler/indexerConstraints2.ts(26,5): error TS2413: Numeric index type 'A' is not assignable to string index type 'B'.
tests/cases/compiler/indexerConstraints2.ts(9,5): error TS2413: Numeric index type '[string]: A' is not assignable to string index type '[string]: B'.
tests/cases/compiler/indexerConstraints2.ts(17,5): error TS2413: Numeric index type '[string]: A' is not assignable to string index type '[string]: B'.
tests/cases/compiler/indexerConstraints2.ts(26,5): error TS2413: Numeric index type '[string]: A' is not assignable to string index type '[string]: B'.


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

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

// With hidden indexer
Expand All @@ -35,6 +35,6 @@ tests/cases/compiler/indexerConstraints2.ts(26,5): error TS2413: Numeric index t
class K extends J {
[n: number]: A;
~~~~~~~~~~~~~~~
!!! error TS2413: Numeric index type 'A' is not assignable to string index type 'B'.
!!! error TS2413: Numeric index type '[string]: A' is not assignable to string index type '[string]: B'.
[s: string]: B;
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
tests/cases/compiler/inheritedStringIndexersFromDifferentBaseTypes2.ts(18,11): error TS2413: Numeric index type '{}' is not assignable to string index type '{ a: any; }'.
tests/cases/compiler/inheritedStringIndexersFromDifferentBaseTypes2.ts(3,5): error TS2413: Numeric index type '[string]: {}' is not assignable to string index type '[string]: { a: any; }'.


==== tests/cases/compiler/inheritedStringIndexersFromDifferentBaseTypes2.ts (1 errors) ====
// indexer in B is a subtype of indexer in A
interface A {
[s: string]: {
~~~~~~~~~~~~~~
a;
~~~~~~~~~~
};
~~~~~~
!!! error TS2413: Numeric index type '[string]: {}' is not assignable to string index type '[string]: { a: any; }'.
}
interface B {
[s: number]: {
Expand All @@ -20,8 +24,6 @@ tests/cases/compiler/inheritedStringIndexersFromDifferentBaseTypes2.ts(18,11): e
[s: number]: {};
}
interface E extends A, D { } // error
~
!!! error TS2413: Numeric index type '{}' is not assignable to string index type '{ a: any; }'.

interface F extends A, D {
[s: number]: {
Expand Down
Loading

0 comments on commit 7b1c496

Please sign in to comment.