Skip to content

Commit e4256d8

Browse files
committed
Merge pull request microsoft#252 from Microsoft/array_symbol_links
Use symbol links to access global array type
2 parents 9e039f5 + a221018 commit e4256d8

3 files changed

Lines changed: 28 additions & 4 deletions

File tree

src/compiler/checker.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2049,14 +2049,29 @@ module ts {
20492049
return <ObjectType>type;
20502050
}
20512051

2052-
function createArrayType(elementType: Type): Type {
2053-
return globalArrayType !== emptyObjectType ? createTypeReference(<GenericType>globalArrayType, [elementType]) : emptyObjectType;
2052+
// arrayType argument is used as a backup in case if globalArrayType is not defined
2053+
function createArrayType(elementType: Type, arrayType?: ObjectType): Type {
2054+
var rootType = globalArrayType || arrayType;
2055+
return rootType !== emptyObjectType ? createTypeReference(<GenericType>rootType, [elementType]) : emptyObjectType;
20542056
}
20552057

20562058
function getTypeFromArrayTypeNode(node: ArrayTypeNode): Type {
20572059
var links = getNodeLinks(node);
20582060
if (!links.resolvedType) {
2059-
links.resolvedType = createArrayType(getTypeFromTypeNode(node.elementType));
2061+
var arrayType = globalArrayType;
2062+
if (!arrayType) {
2063+
// if user code contains augmentation for Array type that includes call\construct signatures with arrays as parameter\return types,
2064+
// then we might step here then during initialization of the global Array type when globalArrayType is not yet set.
2065+
// CODE: interface Array<T> { (): number[] }
2066+
// in this case just resolve name 'Array' again and get declared type of symbol.
2067+
// this type is the one that eventually should be set as 'globalArrayType'.
2068+
// NOTE: this is specific to signatures since got signatures we realize parameter\return types.
2069+
var arrayTypeSymbol = resolveName(node, "Array", SymbolFlags.Type, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined);
2070+
Debug.assert(arrayTypeSymbol);
2071+
arrayType = getDeclaredTypeOfSymbol(arrayTypeSymbol);
2072+
Debug.assert(arrayType);
2073+
}
2074+
links.resolvedType = createArrayType(getTypeFromTypeNode(node.elementType), arrayType);
20602075
}
20612076
return links.resolvedType;
20622077
}
@@ -6281,9 +6296,9 @@ module ts {
62816296
getSymbolLinks(unknownSymbol).type = unknownType;
62826297
globals[undefinedSymbol.name] = undefinedSymbol;
62836298
// Initialize special types
6299+
globalArrayType = getGlobalType("Array", 1);
62846300
globalObjectType = getGlobalType("Object");
62856301
globalFunctionType = getGlobalType("Function");
6286-
globalArrayType = getGlobalType("Array", 1);
62876302
globalStringType = getGlobalType("String");
62886303
globalNumberType = getGlobalType("Number");
62896304
globalBooleanType = getGlobalType("Boolean");
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//// [augmentArray.ts]
2+
interface Array<T> {
3+
(): any[];
4+
}
5+
6+
//// [augmentArray.js]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
interface Array<T> {
2+
(): any[];
3+
}

0 commit comments

Comments
 (0)