Microsoft/TypeScript
False positive on destructured `require` is `verbatimModuleSyntax` and `module` is `preserve`
開放
#63,696 建立於 2026年7月30日
BugHelp Wanted
倉庫指標
- 星標
- (108,860 顆星)
- PR 合併指標
- (平均合併 6天 17小時) (30 天內合併 9 個 PR)
描述
🔎 Search Terms
verbatimModuleSyntax destructure
🕗 Version & Regression Information
- This changed between versions 5.5 and 5.6.
⏯ Playground Link
💻 Code
// file.cjs
const { readFile } = require('node:fs')
// tsconfig.json
{
"compilerOptions": {
"checkJs": true,
"module": "preserve",
"noEmit": true,
"skipLibCheck": true,
"target": "esnext",
"types": ["node"],
"verbatimModuleSyntax": true,
}
}
🙁 Actual behavior
When verbatimModuleSyntax is enabled and module is set to preserve, TypeScript disallows destructuring of a required module in CommonJS JavaScript.
$ tsc
changed 1 package in 544ms
file.cjs:1:9 - error TS1293: ESM syntax is not allowed in a CommonJS module when 'module' is set to 'preserve'.
1 const { readFile } = require('node:fs')
~~~~~~~~
Found 1 error in file.cjs:1
🙂 Expected behavior
No errors
Additional information about the issue
I could not reproduce this in the playground, because ATA is broken.