Skip to content

Relax index signature checks for type any #4074

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5251,7 +5251,7 @@ namespace ts {
return indexTypesIdenticalTo(IndexKind.String, source, target);
}
let targetType = getIndexTypeOfType(target, IndexKind.String);
if (targetType) {
if (targetType && !(targetType.flags & TypeFlags.Any)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use isTypeAny

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would just add function call and a redundant null check.

let sourceType = getIndexTypeOfType(source, IndexKind.String);
if (!sourceType) {
if (reportErrors) {
Expand All @@ -5276,7 +5276,7 @@ namespace ts {
return indexTypesIdenticalTo(IndexKind.Number, source, target);
}
let targetType = getIndexTypeOfType(target, IndexKind.Number);
if (targetType) {
if (targetType && !(targetType.flags & TypeFlags.Any)) {
let sourceStringType = getIndexTypeOfType(source, IndexKind.String);
let sourceNumberType = getIndexTypeOfType(source, IndexKind.Number);
if (!(sourceStringType || sourceNumberType)) {
Expand Down
21 changes: 12 additions & 9 deletions tests/baselines/reference/assignmentCompat1.errors.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
tests/cases/compiler/assignmentCompat1.ts(4,1): error TS2322: Type '{ [index: string]: any; }' is not assignable to type '{ one: number; }'.
Property 'one' is missing in type '{ [index: string]: any; }'.
tests/cases/compiler/assignmentCompat1.ts(5,1): error TS2322: Type '{ one: number; }' is not assignable to type '{ [index: string]: any; }'.
Index signature is missing in type '{ one: number; }'.
tests/cases/compiler/assignmentCompat1.ts(6,1): error TS2322: Type '{ [index: number]: any; }' is not assignable to type '{ one: number; }'.
Property 'one' is missing in type '{ [index: number]: any; }'.


==== tests/cases/compiler/assignmentCompat1.ts (2 errors) ====
var x = {one: 1};
var y: {[index:string]: any};

x = y;
var x = { one: 1 };
var y: { [index: string]: any };
var z: { [index: number]: any };
x = y; // Error
~
!!! error TS2322: Type '{ [index: string]: any; }' is not assignable to type '{ one: number; }'.
!!! error TS2322: Property 'one' is missing in type '{ [index: string]: any; }'.
y = x;
y = x; // Ok because index signature type is any
x = z; // Error
~
!!! error TS2322: Type '{ one: number; }' is not assignable to type '{ [index: string]: any; }'.
!!! error TS2322: Index signature is missing in type '{ one: number; }'.
!!! error TS2322: Type '{ [index: number]: any; }' is not assignable to type '{ one: number; }'.
!!! error TS2322: Property 'one' is missing in type '{ [index: number]: any; }'.
z = x; // Ok because index signature type is any

20 changes: 13 additions & 7 deletions tests/baselines/reference/assignmentCompat1.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
//// [assignmentCompat1.ts]
var x = {one: 1};
var y: {[index:string]: any};

x = y;
y = x;
var x = { one: 1 };
var y: { [index: string]: any };
var z: { [index: number]: any };
x = y; // Error
y = x; // Ok because index signature type is any
x = z; // Error
z = x; // Ok because index signature type is any


//// [assignmentCompat1.js]
var x = { one: 1 };
var y;
x = y;
y = x;
var z;
x = y; // Error
y = x; // Ok because index signature type is any
x = z; // Error
z = x; // Ok because index signature type is any
17 changes: 0 additions & 17 deletions tests/baselines/reference/assignmentCompatability36.errors.txt

This file was deleted.

39 changes: 39 additions & 0 deletions tests/baselines/reference/assignmentCompatability36.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
=== tests/cases/compiler/assignmentCompatability36.ts ===
module __test1__ {
>__test1__ : Symbol(__test1__, Decl(assignmentCompatability36.ts, 0, 0))

export interface interfaceWithPublicAndOptional<T,U> { one: T; two?: U; }; var obj4: interfaceWithPublicAndOptional<number,string> = { one: 1 };;
>interfaceWithPublicAndOptional : Symbol(interfaceWithPublicAndOptional, Decl(assignmentCompatability36.ts, 0, 18))
>T : Symbol(T, Decl(assignmentCompatability36.ts, 1, 52))
>U : Symbol(U, Decl(assignmentCompatability36.ts, 1, 54))
>one : Symbol(one, Decl(assignmentCompatability36.ts, 1, 58))
>T : Symbol(T, Decl(assignmentCompatability36.ts, 1, 52))
>two : Symbol(two, Decl(assignmentCompatability36.ts, 1, 66))
>U : Symbol(U, Decl(assignmentCompatability36.ts, 1, 54))
>obj4 : Symbol(obj4, Decl(assignmentCompatability36.ts, 1, 83))
>interfaceWithPublicAndOptional : Symbol(interfaceWithPublicAndOptional, Decl(assignmentCompatability36.ts, 0, 18))
>one : Symbol(one, Decl(assignmentCompatability36.ts, 1, 139))

export var __val__obj4 = obj4;
>__val__obj4 : Symbol(__val__obj4, Decl(assignmentCompatability36.ts, 2, 14))
>obj4 : Symbol(obj4, Decl(assignmentCompatability36.ts, 1, 83))
}
module __test2__ {
>__test2__ : Symbol(__test2__, Decl(assignmentCompatability36.ts, 3, 1))

export var aa:{[index:string]:any;};;
>aa : Symbol(aa, Decl(assignmentCompatability36.ts, 5, 14))
>index : Symbol(index, Decl(assignmentCompatability36.ts, 5, 20))

export var __val__aa = aa;
>__val__aa : Symbol(__val__aa, Decl(assignmentCompatability36.ts, 6, 14))
>aa : Symbol(aa, Decl(assignmentCompatability36.ts, 5, 14))
}
__test2__.__val__aa = __test1__.__val__obj4
>__test2__.__val__aa : Symbol(__test2__.__val__aa, Decl(assignmentCompatability36.ts, 6, 14))
>__test2__ : Symbol(__test2__, Decl(assignmentCompatability36.ts, 3, 1))
>__val__aa : Symbol(__test2__.__val__aa, Decl(assignmentCompatability36.ts, 6, 14))
>__test1__.__val__obj4 : Symbol(__test1__.__val__obj4, Decl(assignmentCompatability36.ts, 2, 14))
>__test1__ : Symbol(__test1__, Decl(assignmentCompatability36.ts, 0, 0))
>__val__obj4 : Symbol(__test1__.__val__obj4, Decl(assignmentCompatability36.ts, 2, 14))

42 changes: 42 additions & 0 deletions tests/baselines/reference/assignmentCompatability36.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
=== tests/cases/compiler/assignmentCompatability36.ts ===
module __test1__ {
>__test1__ : typeof __test1__

export interface interfaceWithPublicAndOptional<T,U> { one: T; two?: U; }; var obj4: interfaceWithPublicAndOptional<number,string> = { one: 1 };;
>interfaceWithPublicAndOptional : interfaceWithPublicAndOptional<T, U>
>T : T
>U : U
>one : T
>T : T
>two : U
>U : U
>obj4 : interfaceWithPublicAndOptional<number, string>
>interfaceWithPublicAndOptional : interfaceWithPublicAndOptional<T, U>
>{ one: 1 } : { one: number; }
>one : number
>1 : number

export var __val__obj4 = obj4;
>__val__obj4 : interfaceWithPublicAndOptional<number, string>
>obj4 : interfaceWithPublicAndOptional<number, string>
}
module __test2__ {
>__test2__ : typeof __test2__

export var aa:{[index:string]:any;};;
>aa : { [index: string]: any; }
>index : string

export var __val__aa = aa;
>__val__aa : { [index: string]: any; }
>aa : { [index: string]: any; }
}
__test2__.__val__aa = __test1__.__val__obj4
>__test2__.__val__aa = __test1__.__val__obj4 : __test1__.interfaceWithPublicAndOptional<number, string>
>__test2__.__val__aa : { [index: string]: any; }
>__test2__ : typeof __test2__
>__val__aa : { [index: string]: any; }
>__test1__.__val__obj4 : __test1__.interfaceWithPublicAndOptional<number, string>
>__test1__ : typeof __test1__
>__val__obj4 : __test1__.interfaceWithPublicAndOptional<number, string>

5 changes: 1 addition & 4 deletions tests/baselines/reference/indexTypeCheck.errors.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
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(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' or 'number'.
tests/cases/compiler/indexTypeCheck.ts(51,1): error TS2342: An index expression argument must be of type 'string', 'number', 'symbol, or 'any'.


==== tests/cases/compiler/indexTypeCheck.ts (8 errors) ====
==== tests/cases/compiler/indexTypeCheck.ts (7 errors) ====
interface Red {
[n:number]; // ok
~~~~~~~~~~~
Expand Down Expand Up @@ -37,8 +36,6 @@ tests/cases/compiler/indexTypeCheck.ts(51,1): error TS2342: An index expression

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

Expand Down
42 changes: 1 addition & 41 deletions tests/baselines/reference/intTypeCheck.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,7 @@ tests/cases/compiler/intTypeCheck.ts(134,5): error TS2322: Type 'boolean' is not
tests/cases/compiler/intTypeCheck.ts(134,21): error TS1109: Expression expected.
tests/cases/compiler/intTypeCheck.ts(134,22): error TS2304: Cannot find name 'i3'.
tests/cases/compiler/intTypeCheck.ts(135,17): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.
tests/cases/compiler/intTypeCheck.ts(141,5): error TS2322: Type 'Object' is not assignable to type 'i4'.
Index signature is missing in type 'Object'.
tests/cases/compiler/intTypeCheck.ts(142,17): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.
tests/cases/compiler/intTypeCheck.ts(143,5): error TS2322: Type 'Base' is not assignable to type 'i4'.
Index signature is missing in type 'Base'.
tests/cases/compiler/intTypeCheck.ts(145,5): error TS2322: Type '() => void' is not assignable to type 'i4'.
Index signature is missing in type '() => void'.
tests/cases/compiler/intTypeCheck.ts(148,5): error TS2322: Type 'boolean' is not assignable to type 'i4'.
Index signature is missing in type 'Boolean'.
tests/cases/compiler/intTypeCheck.ts(148,21): error TS1109: Expression expected.
tests/cases/compiler/intTypeCheck.ts(148,22): error TS2304: Cannot find name 'i4'.
tests/cases/compiler/intTypeCheck.ts(149,17): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.
Expand Down Expand Up @@ -73,21 +65,13 @@ tests/cases/compiler/intTypeCheck.ts(190,5): error TS2322: Type 'boolean' is not
tests/cases/compiler/intTypeCheck.ts(190,21): error TS1109: Expression expected.
tests/cases/compiler/intTypeCheck.ts(190,22): error TS2304: Cannot find name 'i7'.
tests/cases/compiler/intTypeCheck.ts(191,17): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.
tests/cases/compiler/intTypeCheck.ts(197,5): error TS2322: Type 'Object' is not assignable to type 'i8'.
Index signature is missing in type 'Object'.
tests/cases/compiler/intTypeCheck.ts(198,17): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.
tests/cases/compiler/intTypeCheck.ts(199,5): error TS2322: Type 'Base' is not assignable to type 'i8'.
Index signature is missing in type 'Base'.
tests/cases/compiler/intTypeCheck.ts(201,5): error TS2322: Type '() => void' is not assignable to type 'i8'.
Index signature is missing in type '() => void'.
tests/cases/compiler/intTypeCheck.ts(204,5): error TS2322: Type 'boolean' is not assignable to type 'i8'.
Index signature is missing in type 'Boolean'.
tests/cases/compiler/intTypeCheck.ts(204,21): error TS1109: Expression expected.
tests/cases/compiler/intTypeCheck.ts(204,22): error TS2304: Cannot find name 'i8'.
tests/cases/compiler/intTypeCheck.ts(205,17): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.


==== tests/cases/compiler/intTypeCheck.ts (69 errors) ====
==== tests/cases/compiler/intTypeCheck.ts (61 errors) ====
interface i1 {
//Property Signatures
p;
Expand Down Expand Up @@ -287,27 +271,15 @@ tests/cases/compiler/intTypeCheck.ts(205,17): error TS2351: Cannot use 'new' wit
var obj33: i4;
var obj34: i4 = {};
var obj35: i4 = new Object();
~~~~~
!!! error TS2322: Type 'Object' is not assignable to type 'i4'.
!!! error TS2322: Index signature is missing in type 'Object'.
var obj36: i4 = new obj33;
~~~~~~~~~
!!! error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.
var obj37: i4 = new Base;
~~~~~
!!! error TS2322: Type 'Base' is not assignable to type 'i4'.
!!! error TS2322: Index signature is missing in type 'Base'.
var obj38: i4 = null;
var obj39: i4 = function () { };
~~~~~
!!! error TS2322: Type '() => void' is not assignable to type 'i4'.
!!! error TS2322: Index signature is missing in type '() => void'.
//var obj40: i4 = function foo() { };
var obj41: i4 = <i4> anyVar;
var obj42: i4 = new <i4> anyVar;
~~~~~
!!! error TS2322: Type 'boolean' is not assignable to type 'i4'.
!!! error TS2322: Index signature is missing in type 'Boolean'.
~
!!! error TS1109: Expression expected.
~~
Expand Down Expand Up @@ -421,27 +393,15 @@ tests/cases/compiler/intTypeCheck.ts(205,17): error TS2351: Cannot use 'new' wit
var obj77: i8;
var obj78: i8 = {};
var obj79: i8 = new Object();
~~~~~
!!! error TS2322: Type 'Object' is not assignable to type 'i8'.
!!! error TS2322: Index signature is missing in type 'Object'.
var obj80: i8 = new obj77;
~~~~~~~~~
!!! error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.
var obj81: i8 = new Base;
~~~~~
!!! error TS2322: Type 'Base' is not assignable to type 'i8'.
!!! error TS2322: Index signature is missing in type 'Base'.
var obj82: i8 = null;
var obj83: i8 = function () { };
~~~~~
!!! error TS2322: Type '() => void' is not assignable to type 'i8'.
!!! error TS2322: Index signature is missing in type '() => void'.
//var obj84: i8 = function foo() { };
var obj85: i8 = <i8> anyVar;
var obj86: i8 = new <i8> anyVar;
~~~~~
!!! error TS2322: Type 'boolean' is not assignable to type 'i8'.
!!! error TS2322: Index signature is missing in type 'Boolean'.
~
!!! error TS1109: Expression expected.
~~
Expand Down
12 changes: 7 additions & 5 deletions tests/cases/compiler/assignmentCompat1.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var x = {one: 1};
var y: {[index:string]: any};

x = y;
y = x;
var x = { one: 1 };
var y: { [index: string]: any };
var z: { [index: number]: any };
x = y; // Error
y = x; // Ok because index signature type is any
x = z; // Error
z = x; // Ok because index signature type is any