TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

"Debug Failure. False expression." when using default export from js file

Open literalpie opened this issue 1 year ago • 5 comments

🔎 Search Terms

debug failure false expression export default composite allowjs js

🕗 Version & Regression Information

  • This changed between versions 5.6.2 and 5.7.2

⏯ Playground Link

https://stackblitz.com/edit/ts-false-expression-tqxlxy?file=tsconfig.json,package.json

💻 Code

// In a JS file
export default console; 

The error also seems to happen if you replace console with anything from globalThis (screen, scroll, fetch, etc.)

TSConfig.json:

    "composite": true,
    "allowJs": true,

🙁 Actual behavior

Running tsc results in this error:

> tsc

Error: Debug Failure. False expression.
    at Object.isUndefinedIdentifierExpression (/home/projects/ts-false-expression-tqxlxy/node_modules/typescript/lib/_tsc.js:50211:15)
    at typeFromExpression (/home/projects/ts-false-expression-tqxlxy/node_modules/typescript/lib/_tsc.js:132377:22)
    at serializeTypeOfExpression (/home/projects/ts-false-expression-tqxlxy/node_modules/typescript/lib/_tsc.js:132107:20)
    at Object.serializeTypeOfDeclaration (/home/projects/ts-false-expression-tqxlxy/node_modules/typescript/lib/_tsc.js:132131:16)
    at serializeTypeForDeclaration (/home/projects/ts-false-expression-tqxlxy/node_modules/typescript/lib/_tsc.js:52640:41)
    at serializeMaybeAliasAssignment (/home/projects/ts-false-expression-tqxlxy/node_modules/typescript/lib/_tsc.js:54015:19)
    at serializeAsAlias (/home/projects/ts-false-expression-tqxlxy/node_modules/typescript/lib/_tsc.js:53899:13)
    at serializeSymbolWorker (/home/projects/ts-false-expression-tqxlxy/node_modules/typescript/lib/_tsc.js:53255:11)
    at serializeSymbol (/home/projects/ts-false-expression-tqxlxy/node_modules/typescript/lib/_tsc.js:53119:11)
    at eval (/home/projects/ts-false-expression-tqxlxy/node_modules/typescript/lib/_tsc.js:53089:11)

Node.js v18.20.3

🙂 Expected behavior

No Error :-)

Additional information about the issue

I see this both locally and in stackblitz. I don't know how to reproduce in the playground because it only seems to happen when using a JS file.

This does not cause an error:

const thing = console;
export default thing;

literalpie avatar Dec 05 '24 14:12 literalpie

The playground can be set to Javascript, and does seem to get the same error: https://www.typescriptlang.org/play/?filetype=js#code/KYDwDg9gTgLgBAE2AMwIYFcA28DuBLAOwQhwG4AoIA

tsMode.js:8 Uncaught (in promise) Error: Debug Failure. False expression.
    at Object.isUndefinedIdentifierExpression (tsWorker.js:344:245998)
    at Z (tsWorker.js:362:12320)
    at h (tsWorker.js:362:8628)
    at Object._ [as serializeTypeOfDeclaration] (tsWorker.js:362:8889)
    at g0 (tsWorker.js:348:14998)
    at hM (tsWorker.js:351:9451)
    at y0 (tsWorker.js:351:8083)
    at jr (tsWorker.js:348:24089)
    at Vn (tsWorker.js:348:21713)
    at tsWorker.js:348:21375

nmain avatar Dec 05 '24 14:12 nmain

via bisect, I've determined the problem was introduced in https://github.com/microsoft/TypeScript/pull/59282 and is likely in src/compiler/expressionToTypeNode.ts somewhere. unfortunately, that's about as far as I can get. cc @dragomirtitian

boneskull avatar Feb 12 '25 23:02 boneskull

OK, I lied, I got closer. The switch in isExpressionNode is failing here:

https://github.com/microsoft/TypeScript/blob/476e9ee201bd19afbc359ffe93b32a0ccd97152a/src/compiler/utilities.ts#L3604-L3609

Given:

export default baz

where baz was not defined anywhere in the module (so it must be globalThis.baz), we find that baz is an Identifier, and its parent is an ExportAssignment (default), so it does not return true here, and falls through to the bottom of the switch, returning false and failing the assertion.

boneskull avatar Feb 12 '25 23:02 boneskull

I'm wondering if there will be a fix for this or should I start looking to change my own code to try to solve this? I am unable to upgrade because of this

alebrozzo avatar Mar 14 '25 14:03 alebrozzo

Personally, I did a workaround. It's pretty simple since it works as long as you set the value before you export it

// Change from: export default console;
// To:
const myConsole = console;
export default myConsole;

literalpie avatar Mar 14 '25 19:03 literalpie

I had to patch the package in order to skip this check so that my bundler can work. It's not a good idea, though.

diff --git a/lib/typescript.js b/lib/typescript.js
index dc0fe9a56bb4d9b08efe8b5948a60a74a4e9997b..3949bd42b5d2f120c1f1521de685164884ed1d7a 100755
--- a/lib/typescript.js	
+++ b/lib/typescript.js	
@@ -55006,7 +55006,7 @@ function createTypeChecker(host) {
       },
       isOptionalParameter,
       isUndefinedIdentifierExpression(node) {
-        Debug.assert(isExpressionNode(node));
+        // Debug.assert(isExpressionNode(node)); // To get rid of: [!] (plugin typescript) Error: Debug Failure. False expression.
         return getSymbolAtLocation(node) === undefinedSymbol;
       },
       isEntityNameVisible(context, entityName, shouldComputeAliasToMakeVisible) {

NotYoojun avatar Apr 02 '25 10:04 NotYoojun

I'm getting the exact same error in my code. Also began in TS 5.7

bolatovumar avatar Apr 04 '25 04:04 bolatovumar

This was fixed in https://github.com/microsoft/TypeScript/pull/61582 by @andrewbranch. I can reproduce in v5.8.3, but not main.

boneskull avatar Apr 24 '25 20:04 boneskull