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

Destructuring exported array with require causes error #44658

Open
lumaxis opened this issue Jun 18, 2021 · 3 comments
Open

Destructuring exported array with require causes error #44658

lumaxis opened this issue Jun 18, 2021 · 3 comments
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@lumaxis
Copy link

lumaxis commented Jun 18, 2021

Bug Report

I'm progressively adding TypeScript to an existing JS codebase. In several of our files, there is an existing pattern of exporting arrays like so: module.exports = [A, serializeA].
When requiring and destructuring this export like const [A] = require('./a'), I get an unexpected error Module '"./a"' has no exported member 'A'. ts(2305) which I believe to be a bug.

🔎 Search Terms

module has no exported member TS2305

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about module.exports

⏯ Playground Link

Not sure how to repro this in a Playground work Workbench since I believe it requires files and exports/requries.

💻 Code

// a.js
class A {}
module.exports = [A]

// b.js
const [A] = require('./a') // Module '"./a"' has no exported member 'A'. ts(2305)

🙁 Actual behavior

The compiler shows an error

🙂 Expected behavior

The compiler shows no error and the exported array is correctly destructured and the type infered.

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Jun 18, 2021
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Jun 18, 2021
@RyanCavanaugh RyanCavanaugh added the Help Wanted You can do this label Jun 18, 2021
@RyanCavanaugh
Copy link
Member

Seems like we missed a case in the destructuring logic where we forgot to check whether it's an array or an object?

@assapir
Copy link

assapir commented Jun 29, 2021

I would like to take a look at this, can I have some direction on where to start looking at it?

@MarcosAtApprentice
Copy link

Just ran into this issue, very tricky to figure out the source of this. Nothing was working for type inference, until I tried first assigning the result of require() into a variable and only then destructuring that variable.

Using latest version:

image

This works:

image

but this doesn't work:

image

Having to require() into a variable, then destructuring that variable for it work doesn't seem like intended behavior.

I also found someone having the same issue who found the same workaround in this Stack Overflow thread.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

4 participants