Skip to content

Commit 0dcc168

Browse files
committed
Merge pull request #1815 from Microsoft/objectLiteralIndexerNoImplicitAny
Use transient symbols when computing the indexers for object literals
2 parents 17b19bf + ca8c446 commit 0dcc168

File tree

4 files changed

+50
-7
lines changed

4 files changed

+50
-7
lines changed

src/compiler/checker.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5441,7 +5441,8 @@ module ts {
54415441
// Grammar checking
54425442
checkGrammarObjectLiteralExpression(node);
54435443

5444-
var properties: SymbolTable = {};
5444+
var propertiesTable: SymbolTable = {};
5445+
var propertiesArray: Symbol[] = [];
54455446
var contextualType = getContextualType(node);
54465447
var typeFlags: TypeFlags;
54475448

@@ -5486,34 +5487,39 @@ module ts {
54865487
}
54875488

54885489
if (!hasDynamicName(memberDecl)) {
5489-
properties[member.name] = member;
5490+
propertiesTable[member.name] = member;
54905491
}
5492+
propertiesArray.push(member);
54915493
}
54925494

54935495
// If object literal is contextually (but not inferentially) typed, copy missing optional properties from
54945496
// the contextual type such that the resulting type becomes a subtype in cases where only optional properties
54955497
// were omitted. There is no need to create new property objects as nothing in them needs to change.
54965498
if (contextualType && !isInferentialContext(contextualMapper)) {
54975499
forEach(getPropertiesOfObjectType(contextualType), p => {
5498-
if (p.flags & SymbolFlags.Optional && !hasProperty(properties, p.name)) {
5499-
properties[p.name] = p;
5500+
if (p.flags & SymbolFlags.Optional && !hasProperty(propertiesTable, p.name)) {
5501+
propertiesTable[p.name] = p;
55005502
}
55015503
});
55025504
}
55035505

55045506
var stringIndexType = getIndexType(IndexKind.String);
55055507
var numberIndexType = getIndexType(IndexKind.Number);
5506-
var result = createAnonymousType(node.symbol, properties, emptyArray, emptyArray, stringIndexType, numberIndexType);
5508+
var result = createAnonymousType(node.symbol, propertiesTable, emptyArray, emptyArray, stringIndexType, numberIndexType);
55075509
result.flags |= (typeFlags & TypeFlags.Unwidened);
55085510
return result;
55095511

55105512
function getIndexType(kind: IndexKind) {
55115513
if (contextualType && contextualTypeHasIndexSignature(contextualType, kind)) {
55125514
var propTypes: Type[] = [];
5513-
for (var i = 0; i < node.properties.length; i++) {
5515+
for (var i = 0; i < propertiesArray.length; i++) {
55145516
var propertyDecl = node.properties[i];
55155517
if (kind === IndexKind.String || isNumericName(propertyDecl.name)) {
5516-
var type = getTypeOfSymbol(getSymbolOfNode(propertyDecl));
5518+
// Do not call getSymbolOfNode(propertyDecl), as that will get the
5519+
// original symbol for the node. We actually want to get the symbol
5520+
// created by checkObjectLiteral, since that will be appropriately
5521+
// contextually typed and resolved.
5522+
var type = getTypeOfSymbol(propertiesArray[i]);
55175523
if (!contains(propTypes, type)) {
55185524
propTypes.push(type);
55195525
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [objectLiteralIndexerNoImplicitAny.ts]
2+
interface I {
3+
[s: string]: any;
4+
}
5+
6+
var x: I = {
7+
p: null
8+
}
9+
10+
//// [objectLiteralIndexerNoImplicitAny.js]
11+
var x = {
12+
p: null
13+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=== tests/cases/compiler/objectLiteralIndexerNoImplicitAny.ts ===
2+
interface I {
3+
>I : I
4+
5+
[s: string]: any;
6+
>s : string
7+
}
8+
9+
var x: I = {
10+
>x : I
11+
>I : I
12+
>{ p: null} : { [x: string]: null; p: null; }
13+
14+
p: null
15+
>p : null
16+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//@noImplicitAny: true
2+
interface I {
3+
[s: string]: any;
4+
}
5+
6+
var x: I = {
7+
p: null
8+
}

0 commit comments

Comments
 (0)