Microsoft/TypeScript

Destructuring exported array with `require` causes error

Open

#44,658 建立於 2021年6月18日

在 GitHub 查看
 (3 留言) (1 反應) (0 負責人)TypeScript (6,726 fork)batch import
BugDomain: JavaScriptHelp Wanted

倉庫指標

Star
 (48,455 star)
PR 合併指標
 (平均合併 6天 17小時) (30 天內合併 9 個 PR)

描述

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.

貢獻者指南