-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow dynamic usage of import = require
syntax
#60884
Comments
Would that even be legal JavaScript syntax? |
Why wouldn't it? A |
This comment has been minimized.
This comment has been minimized.
Oh, I understand the question better now. This is existing TS syntax and it's required (pun intended) for CJS + verbatimModuleSyntax. I'm just asking for it to be permitted in all locations where a statement is permitted, rather than just the top level.
|
Miscellaneous example of where this could be used in the wild: Here, the function needs to be both synchronous and lazy, so |
🔍 Search Terms
dynamic import, require, verbatimmodulesyntax
✅ Viability Checklist
⭐ Suggestion
I propose that
import = require()
should be allowed anywhere arequire()
statement is allowed (under appropriate module settings).📃 Motivating Example
I've been trying to convert a commonjs project to verbatimModuleSyntax. Currently dynamic JSON imports are written as
which are rewritten as
Promise.resolve().then(() => require('./foo.json'));
by TS due to my module setting. Theimport()
syntax currently has the advantage that TS statically analyzes and provides types for the imported JSON.I'd like to convert the JSON import to a
require()
call, however, a barerequire()
loses static analysis. Therefore, I'd like to be able to usein order to have type inference and static analysis of the json file (also to have it copied to the output build).
(using a node
import()
-from-CJS is not available to me at this time)💻 Use Cases
require()
s.Somewhat relates to #60598
The text was updated successfully, but these errors were encountered: