TypeScript
TypeScript copied to clipboard
Stack overflow on recursive type alias
Code to reproduce:
//interface TreeNode {
type TreeNode = {
name: string;
parent: TreeNode;
}
function getNames(nodes: TreeNode[]): string[] {
return nodes.map(s => s.name);
}
Compilation error:
(function (exports, require, module, __filename, __dirname) { /*! ************
^
RangeError: Maximum call stack size exceeded
at instantiateAnonymousType (C:\Users\Koloto\AppData\Roaming\npm\node_modules\typescript\bin\tsc.js)
at instantiateType (C:\Users\Koloto\AppData\Roaming\npm\node_modules\typescript\bin\tsc.js:11775:25)
at getTypeOfInstantiatedSymbol (C:\Users\Koloto\AppData\Roaming\npm\node_modules\typescript\bin\tsc.js:10500:30)
at getTypeOfSymbol (C:\Users\Koloto\AppData\Roaming\npm\node_modules\typescript\bin\tsc.js:10506:24)
at inferFromProperties (C:\Users\Koloto\AppData\Roaming\npm\node_modules\typescript\bin\tsc.js:12766:40)
at inferFromTypes (C:\Users\Koloto\AppData\Roaming\npm\node_modules\typescript\bin\tsc.js:12750:25)
at inferFromProperties (C:\Users\Koloto\AppData\Roaming\npm\node_modules\typescript\bin\tsc.js:12766:25)
at inferFromTypes (C:\Users\Koloto\AppData\Roaming\npm\node_modules\typescript\bin\tsc.js:12750:25)
at inferFromProperties (C:\Users\Koloto\AppData\Roaming\npm\node_modules\typescript\bin\tsc.js:12766:25)
at inferFromTypes (C:\Users\Koloto\AppData\Roaming\npm\node_modules\typescript\bin\tsc.js:12750:25)
If you replace type with interface (uncomment the first line and comment the second), the code is compiled successfully.
I use version 1.5.3, also reproduced in 1.4.
Confirmed in the latest nightly (1.6.0-dev.20150825).
More minimal repro:
type TreeNode = {
name: string;
parent: TreeNode;
}
var nodes: TreeNode[];
nodes.map(n => n.name);
Seems like the invocation of map is what's doing it.
@DanielRosenwasser can you take a look.
BTW: It's the bug we encountered in #3451 and finally managed to reproduce.