Skip to content
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

Namespace import in esm for cjs module described by any type leads to debug failure #51099

Closed
dsherret opened this issue Oct 7, 2022 · 4 comments · Fixed by #51136
Closed
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Rescheduled This issue was previously scheduled to an earlier milestone

Comments

@dsherret
Copy link
Contributor

dsherret commented Oct 7, 2022

Bug Report

Namespace importing a cjs module described with an any type from an es module leads to a debug failure.

🔎 Search Terms

Debug Failure. Unhandled type 1

🕗 Version & Regression Information

  • This is a crash in 4.9.0-dev.20221007 and 4.8.4

⏯ Playground Link

Can't because it's multiple files.

💻 Code

package.json

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "type": "module",
  "module": "index.mjs",
  "dependencies": {
    "typescript": "^4.9.0-dev.20221007"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "target": "ESNext",
    "module": "ESNext",
    "moduleResolution": "NodeNext",
    "strict": true
  }
}

index.mts

import * as namespaceImport from "./other.cjs";
console.log(namespaceImport);

other.cjs

// note: this is described by other.d.cts
module.exports.test = () => 5;

other.d.cts

declare const __: any;
export = __;

🙁 Actual behavior

Running tsc:

V:\any-cjs-tsc\node_modules\typescript\lib\tsc.js:99472
                throw e;
                ^

Error: Debug Failure. Unhandled type 1
    at resolveStructuredTypeMembers (V:\any-cjs-tsc\node_modules\typescript\lib\tsc.js:49660:30)
    at cloneTypeAsModuleType (V:\any-cjs-tsc\node_modules\typescript\lib\tsc.js:43230:38)
    at resolveESModuleSymbol (V:\any-cjs-tsc\node_modules\typescript\lib\tsc.js:43209:36)
    at getTargetOfNamespaceImport (V:\any-cjs-tsc\node_modules\typescript\lib\tsc.js:42412:28)
    at getTargetOfAliasDeclaration (V:\any-cjs-tsc\node_modules\typescript\lib\tsc.js:42640:28)
    at resolveAlias (V:\any-cjs-tsc\node_modules\typescript\lib\tsc.js:42681:30)
    at checkAliasSymbol (V:\any-cjs-tsc\node_modules\typescript\lib\tsc.js:72981:26)
    at checkImportBinding (V:\any-cjs-tsc\node_modules\typescript\lib\tsc.js:73096:13)
    at checkImportDeclaration (V:\any-cjs-tsc\node_modules\typescript\lib\tsc.js:73147:29)
    at checkSourceElementWorker (V:\any-cjs-tsc\node_modules\typescript\lib\tsc.js:73614:28)

Note: type 1 is TypeFlags.Any

🙂 Expected behavior

Should pass type checking.

Why?

We use the file with declare const __: any; export = __ in Deno in order to give certain modules an any type. Now when adding impliedNodeFormat when creating a source file in order to allow importing npm packages, I had to make this module CJS because it has export = and that surfaced this issue.

@dsherret dsherret changed the title Namespace import in esm module for cjs module described by any type leads to debug failure Namespace import in esm for cjs module described by any type leads to debug failure Oct 7, 2022
@jakebailey
Copy link
Member

I can reproduce this, but the bisect will certainly show this to be #49943 where I added the assert.

any is not a StructuredType so it's not right for any to be there; cloneTypeAsModuleType casts the module type to StructuredType so this fits with the bug. Probably, it should check if the export is actually a structured type; it only cares because it aims to remove the call signatures from said type but that's not helpful when the type is already any.

@jakebailey
Copy link
Member

Also, the same thing happens when the export is unknown, symbol, etc, for the same reason.

@jakebailey
Copy link
Member

jakebailey commented Oct 10, 2022

I should note that I also don't know the semantics of this:

    "target": "ESNext",
    "module": "ESNext",
    "moduleResolution": "NodeNext",

As that's something we have a PR to actively ban (#50985), but I don't know if that affects this issue.

I'm not really sure off the top of my head what you can actually get as an export object when it's a * import; can it only ever be an structured object and not say, symbol?

@jakebailey
Copy link
Member

I opened #51136 since I had the code, but I think someone with better knowledge about the workings of these kinds of imports should weigh in. (I'm getting to this pre-triage, sorry!)

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Oct 12, 2022
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.9.2 milestone Oct 12, 2022
@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Oct 20, 2022
@RyanCavanaugh RyanCavanaugh added the Rescheduled This issue was previously scheduled to an earlier milestone label Feb 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Rescheduled This issue was previously scheduled to an earlier milestone
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants