TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Stack overflow on recursive type alias

Open Koloto opened this issue 11 years ago • 4 comments

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.

Koloto avatar Aug 25 '15 17:08 Koloto

Confirmed in the latest nightly (1.6.0-dev.20150825).

DanielRosenwasser avatar Aug 25 '15 17:08 DanielRosenwasser

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 avatar Aug 25 '15 17:08 DanielRosenwasser

@DanielRosenwasser can you take a look.

mhegazy avatar Aug 25 '15 19:08 mhegazy

BTW: It's the bug we encountered in #3451 and finally managed to reproduce.

evil-shrike avatar Aug 26 '15 08:08 evil-shrike