Skip to content

API: Exception in TypeChecker.typeToTypeNode #45515

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
tomblind opened this issue Aug 19, 2021 · 0 comments
Open

API: Exception in TypeChecker.typeToTypeNode #45515

tomblind opened this issue Aug 19, 2021 · 0 comments
Assignees
Labels
Needs Investigation This issue needs a team member to investigate its status. Rescheduled This issue was previously scheduled to an earlier milestone

Comments

@tomblind
Copy link

tomblind commented Aug 19, 2021

Bug Report

πŸ”Ž Search Terms

typeToTypeNode kind undefined

πŸ•— Version & Regression Information

4.3.2 and earlier versions (not sure when it started)

  • This is a crash

⏯ Playground Link

Playground

πŸ’» Code

Using compiler API:

function transformVariableDeclaration(statement: ts.VariableDeclaration, checker: ts.TypeChecker) {
    if (statement.initializer) {
        const initializerType = checker.getTypeAtLocation(statement.initializer);
        const initializerTypeNode = checker.typeToTypeNode(initializerType, undefined, undefined); // TypeError: Cannot read property 'kind' of undefined
        // ...
    }
}

Target code being transpiled:

type Foo = (fn: (...args: [][]) => void) => void;
const foo: Foo = (bar => {});

Exception thrown:

Exception has occurred: TypeError: Cannot read property 'kind' of undefined
  at Object.parenthesizeElementTypeOfArrayType (node_modules\typescript\lib\typescript.js:20825:28)
    at Object.createArrayTypeNode (node_modules\typescript\lib\typescript.js:22343:53)
    at typeReferenceToTypeNode (node_modules\typescript\lib\typescript.js:49344:52)
    at typeToTypeNodeHelper (node_modules\typescript\lib\typescript.js:49085:95)
    at serializeTypeForDeclaration (node_modules\typescript\lib\typescript.js:50323:30)
    at symbolToParameterDeclaration (node_modules\typescript\lib\typescript.js:49803:41)
    at node_modules\typescript\lib\typescript.js:49725:263
    at Array.map (<anonymous>)
    at signatureToSignatureDeclarationHelper (node_modules\typescript\lib\typescript.js:49725:229)
    at createTypeNodeFromObjectType (node_modules\typescript\lib\typescript.js:49296:49)

πŸ™ Actual behavior

When calling typeToTypeNode an exception is thrown.

πŸ™‚ Expected behavior

No exception should be thrown and undefined should be returned.

Tracing through the ts code, undefined is returned from typeReferenceToTypeNode here (linking to online editor since github can't display the file) and there is a comment referencing this issue. That is assigned to the object attempting to be accessed.

The trigger seems to be the rest parameter type being an array of empty tuples. The real code that triggered the issue used some tricky generics to arrive at that type, but this direct version was able to reproduce the same issue.

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Aug 19, 2021
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.5.0 milestone Aug 19, 2021
@RyanCavanaugh RyanCavanaugh added the Rescheduled This issue was previously scheduled to an earlier milestone label Dec 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Investigation This issue needs a team member to investigate its status. Rescheduled This issue was previously scheduled to an earlier milestone
Projects
None yet
Development

No branches or pull requests

3 participants