microsoft / microsoft/TypeScript

Destructuring exported array with `require` causes error

Open
#44,658 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: JavaScript Help Wanted
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the two-file reproduction in a.js and b.js, where module.exports assigns an array and require destructures it. Trace the compiler's handling of module.exports and the destructured require to determine why TS2305 is reported. Done means the example compiles without that error and the destructured value's type is inferred correctly.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.