Skip to content

Type with negative properties cause crash in infer return typeΒ #56562

Closed
@dragomirtitian

Description

@dragomirtitian

πŸ”Ž Search Terms

negative numeric literals infer return type

πŸ•— Version & Regression Information

  • This is a crash

⏯ Playground Link

Playground Link

πŸ’» Code

function x() {
    return {
        [-1]: 0
    } as const
}

πŸ™ Actual behavior

When using the refactor Infer function return type nothing happens. Extension host in VS Code shows a stack trace ending with:

Error: Debug Failure. False expression: Token end is child end
    at processChildNode (c:\dev\TSC\tmp\defaults-and-bindings-code-fix\node_modules\typescript\lib\tsserver.js:171847:17)
    at c:\dev\TSC\tmp\defaults-and-bindings-code-fix\node_modules\typescript\lib\tsserver.js:171778:9

πŸ™‚ Expected behavior

Should infer { readonly [-1]: 0 }

Additional information about the issue

Found while working on isolated declarations.

The issue appears to be that the formatter does not deal well with negative numbers in computed properties. This was not a problem before our recent merge with master as the type printer appears to have been producing negative number literals and it now produces the correct UnaryOperator(-, abs(value))

The error occurs in this assertion:

            if (isToken(child)) {
                // if child node is a token, it does not impact indentation, proceed it using parent indentation scope rules
                const tokenInfo = formattingScanner.readTokenInfo(child);
                // JSX text shouldn't affect indenting
                if (child.kind !== SyntaxKind.JsxText) {
                    Debug.assert(tokenInfo.token.end === child.end, "Token end is child end");
                    consumeTokenAndAdvanceScanner(tokenInfo, node, parentDynamicIndentation, child);
                    return inheritedIndentation;
                }
            }

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions