Typescript: compiler crash TypeError: Cannot read property 'type' of undefined when allowJs=true

Created on 16 Jun 2017  ·  8Comments  ·  Source: microsoft/TypeScript

TypeScript Version: 2.5.0-dev.20170616

Attempting to compile with allowJs = true crashes the compiler.
No crash with allowJs = false
I'm not able to localize which .js file causes the problem, as there are a lot.

Expected behavior:
No crash, many code problems reported

Actual behavior:
Compiler crashes, nothing reported, just a stack trace.

% tsc
/usr/lib/node_modules/typescript/lib/tsc.js:55789
                throw e;
                ^

TypeError: Cannot read property 'type' of undefined
    at resolveBaseTypesOfClass (/usr/lib/node_modules/typescript/lib/tsc.js:22130:73)
    at getBaseTypes (/usr/lib/node_modules/typescript/lib/tsc.js:22089:25)
    at checkClassLikeDeclaration (/usr/lib/node_modules/typescript/lib/tsc.js:34332:33)
    at checkClassDeclaration (/usr/lib/node_modules/typescript/lib/tsc.js:34302:13)
    at checkSourceElement (/usr/lib/node_modules/typescript/lib/tsc.js:35211:28)
    at Object.forEach (/usr/lib/node_modules/typescript/lib/tsc.js:289:30)
    at checkSourceFileWorker (/usr/lib/node_modules/typescript/lib/tsc.js:35280:20)
    at checkSourceFile (/usr/lib/node_modules/typescript/lib/tsc.js:35265:13)
    at Object.forEach (/usr/lib/node_modules/typescript/lib/tsc.js:289:30)
    at getDiagnosticsWorker (/usr/lib/node_modules/typescript/lib/tsc.js:35330:16)

Config:

    "compilerOptions": {
        "module": "es6",
        "target": "es5",
        "outDir": "lib",
        "noImplicitAny": false,
        "sourceMap": true,
        "declaration": false,
        "pretty": true,
        "skipLibCheck": false,
        "strictNullChecks": true,
        "allowJs": true
    },

Bug Fixed

Most helpful comment

I've just faced the same problem. Fortunately I was able to create a small reproducer: https://github.com/stas-vilchik/ts-reproducer.

Actually the failure happens only when "allowJs" is true.

Hope this will help!

All 8 comments

If you can provide a minimal repro or even just the project on GitHub we can take a look. We can also figure something out if the code is closed-source.

OK, I was able to reduce the case down to a handful of mostly empty files importing each other.

Here's an interesting tidbit, possibly a clue:
Removing this comment in file app/core/models/Role.js makes the crash go away:

/**
 * @augments Base
 */

Repro with:
tsc @tsargs

tc-16585.zip

Reproduced the same issue (allowJs option enabled) with the following stack track:

TypeError: Cannot read property 'type' of undefined
    at Object.getEffectiveTypeAnnotationNode (C:\Users\hidden\AppData\Roaming\npm\node_modules\typescript\lib\tsc.js:7350:17)
    at assignContextualParameterTypes (C:\Users\hidden\AppData\Roaming\npm\node_modules\typescript\lib\tsc.js:34465:25)
    at checkFunctionExpressionOrObjectLiteralMethod (C:\Users\hidden\AppData\Roaming\npm\node_modules\typescript\lib\tsc.js:34717:29)
    at checkExpressionWorker (C:\Users\hidden\AppData\Roaming\npm\node_modules\typescript\lib\tsc.js:35590:28)
    at checkExpression (C:\Users\hidden\AppData\Roaming\npm\node_modules\typescript\lib\tsc.js:35534:42)
    at checkBinaryLikeExpression (C:\Users\hidden\AppData\Roaming\npm\node_modules\typescript\lib\tsc.js:35183:29)
    at checkBinaryExpression (C:\Users\hidden\AppData\Roaming\npm\node_modules\typescript\lib\tsc.js:35175:20)
    at checkExpressionWorker (C:\Users\hidden\AppData\Roaming\npm\node_modules\typescript\lib\tsc.js:35611:28)
    at checkExpression (C:\Users\hidden\AppData\Roaming\npm\node_modules\typescript\lib\tsc.js:35534:42)
    at checkBinaryLikeExpression (C:\Users\hidden\AppData\Roaming\npm\node_modules\typescript\lib\tsc.js:35183:29)

Running into the same problem. I couldn't locate the file that crashes it unfortunately.

[email protected]

Edit: just notice that I have a different stack trace as @smendola but the same stack trace as @neooleg , probably caused by different reason.

Also, I managed to bypass the issue by directly edit the tsc.js. Changing

                if (!ts.getEffectiveTypeAnnotationNode(parameter.valueDeclaration)) {

to

                if (parameter.valueDeclaration && !ts.getEffectiveTypeAnnotationNode(parameter.valueDeclaration)) {

works for me. It simply bypasses this issue and I don't think this count as a fix.

I've just faced the same problem. Fortunately I was able to create a small reproducer: https://github.com/stas-vilchik/ts-reproducer.

Actually the failure happens only when "allowJs" is true.

Hope this will help!

Thanks @stas-vilchik, I will work from that repro.

The crash happens because using arguments in JS causes a synthetic rest parameter to be created. But the parameter doesn't have a declaration (it's not actually declared). assignContextualParameterTypes assumes that all rest parameters have a declaration, which causes the crash when parameter.valueDeclaration is passed to getEffectiveTypeAnnotationNode.

I think the correct fix is to just make assignContextualParameterTypes know that transient symbols (1) have no declaration (2) should be contextually typed anyway. But I could instead find a node to serve as the "declaration", similar to the way that = is the declaration node for this.prop = value inside a javascript constructor. Then getEffectiveTypeAnnotationNode would need to return undefined for this node.

I'll have a fix up shortly, as soon as I make the repro even smaller.

Fix is up at #18392

Was this page helpful?
0 / 5 - 0 ratings