Skip to content

RangeError: Maximum call stack size exceeded when trying to infer type of a variable reassigned in a loopΒ #63192

@afarnsworth-valve

Description

@afarnsworth-valve

πŸ”Ž Search Terms

"Maximum call stack size exceeded"
"Maximum call stack size exceeded loop"
"loop reassign referenced in its own initializer"

πŸ•— Version & Regression Information

  • This changed between versions 5.3.3 and 5.4.5

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.9.3#code/JYOwLgpgTgZghgYwgAgHIHsAmECwAoAb3wEgEALYAG0yghAH4AuZWuTdESgTzSwgG0AugG4SobAA8myEAFcAtgCNoovAF98+GLJAIwwDsgCSkKHEgZsAZwAUyTObjMCMvs0so1yAJT4ieYkoIMFdsdz5kAB9kHWwYUAhMZABee0cAOhA+VWIAdwog5Dss7B8Sf2JSDisQl3IqGjoAGmRxCAkU5ABaAEZkL1SSiByqkBrkRH0ANwgPcNLo2Ih4kETOto6AQlTe5AAyPeR66loQZHojgsaQfg3BZGYllcTVEmIhzsngGY8cjXUgA

πŸ’» Code

interface Node
{
	children?: readonly Node[];
	index?: number;
}

function IterateNodes( data: { node: Node } )
{
	let node: Node | undefined = data.node;
	while ( node )
	{
		const { children, index = -1 } = node;
		const activeNode: Node | undefined = index != -1 && children ? children[index] : undefined;

		node = activeNode;
	}
}

πŸ™ Actual behavior

tsc will crash with a stack like:

RangeError: Maximum call stack size exceeded
    at checkExpressionWorker (D:\src\steam\main\web_src\node_modules\typescript\lib\typescript.js:85502:33)
    at checkExpression (D:\src\steam\main\web_src\node_modules\typescript\lib\typescript.js:85457:32)
    at checkTruthinessExpression (D:\src\steam\main\web_src\node_modules\typescript\lib\typescript.js:88209:34)
    at checkConditionalExpression (D:\src\steam\main\web_src\node_modules\typescript\lib\typescript.js:85005:18)
    at checkExpressionWorker (D:\src\steam\main\web_src\node_modules\typescript\lib\typescript.js:85595:16)
    at checkExpression (D:\src\steam\main\web_src\node_modules\typescript\lib\typescript.js:85457:32)
    at getTypeOfExpression (D:\src\steam\main\web_src\node_modules\typescript\lib\typescript.js:85398:18)
    at getTypeOfInitializer (D:\src\steam\main\web_src\node_modules\typescript\lib\typescript.js:74377:34)
    at getInitialTypeOfVariableDeclaration (D:\src\steam\main\web_src\node_modules\typescript\lib\typescript.js:74381:14)
    at getInitialType (D:\src\steam\main\web_src\node_modules\typescript\lib\typescript.js:74392:58)

The actual stack varies a bit but generally these functions.

Prior to typescript 5.4.5, this would not crash but would error that children and index are referenced in their own initializer. All of these errors occur only in strict mode.

πŸ™‚ Expected behavior

tsc should not crash but display errors (even though these are somewhat confusing errors)

Additional information about the issue

Was able to resolve the errors by changing the loop variable from let node: NodeType | undefined = data.node; to let node = data.node as NodeType | undefined (playground)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions