Allow destructuring in import assignment #46752
Labels
Awaiting More Feedback
This means we'd like to hear from more people who would be helped by this feature
Suggestion
An idea for TypeScript
Suggestion
import require destructuring
✅ Viability Checklist
My suggestion meets these guidelines:
⭐ Suggestion
When compiling with
--module commonjs
,const ... require ...
andimport ... from ...
both allow destructuring, butimport ... require ...
doesn't. This seems a bit arbitrary, and the errors generated are especially confusing:📃 Motivating Example
After reading the 4.5.0-beta announcement, I was surprised to find that using import assignment was not as simple as replacing
const
withimport
. I was even more surprised to find that this works in both CommonJS and ES6, but not with the syntax mentioned in that announcement.For a specific example in code, see the code above. Currently, all three syntaxes have potentially undesirable consequences:
import ... from ...
with--esModuleInterop
creates extra code in case the module (in this case,fs
) was compiled the same way. However, this is unnecessary.const ... require ...
produces the code hint'require' call may be converted to an import. ts(80005)
import ... require ...
prevents destructuring.💻 Use Cases
This isn't a major issue, as I can work around it by manually referring to
fs.readFileSync
(as opposed to unqualifiedreadFileSync
), or by destructuring explicitly:However, this creates an equally redundant emit:
The text was updated successfully, but these errors were encountered: