π Search Terms
verbatimModuleSyntax destructure
π Version & Regression Information
- This changed between versions 5.5 and 5.6.
β― Playground Link
https://www.typescriptlang.org/play/?moduleResolution=99&target=99&module=199&verbatimModuleSyntax=true&filetype=js#code/PQgEB4CcFMDNpgOwMbVAFwJ4AdoGcBeAIkQHsATaI0YAPgCh7lTE91QBvUGAQ3IDEAlgBs0AX1AFu0AI4BXQTAAUAcjKUAXLDwqAlIwC2FOaIB00AB7ZSkdHknS+Q0fSA
π» Code
// file.cjs
const { readFile } = require('node:fs')
π 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.
π Search Terms
verbatimModuleSyntax destructure
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play/?moduleResolution=99&target=99&module=199&verbatimModuleSyntax=true&filetype=js#code/PQgEB4CcFMDNpgOwMbVAFwJ4AdoGcBeAIkQHsATaI0YAPgCh7lTE91QBvUGAQ3IDEAlgBs0AX1AFu0AI4BXQTAAUAcjKUAXLDwqAlIwC2FOaIB00AB7ZSkdHknS+Q0fSA
π» Code
π Actual behavior
When
verbatimModuleSyntaxis enabled andmoduleis set topreserve, TypeScript disallows destructuring of arequired module in CommonJS JavaScript.π Expected behavior
No errors
Additional information about the issue
I could not reproduce this in the playground, because ATA is broken.