Import assignment should work with esnext targets
See original GitHub issueSearch Terms
import assignment
Suggestion
#22321 was closed by a bot that thought the issue was addressed, but it wasn’t.
Import assignment should work with esnext targets
Use Cases
JS modules loaders for Node might not support importing CJS with import
statements, so you would use require
to import them. But we’d still like to bring in their types. This is what import assignment is designed for, it’s just disallowed with esnext targets.
Examples
This:
import * as m from './a-module.js';
import cjs = require('some-cjs');
should emit:
import * as m from './a-module.js';
const cjs = require('some-cjs');
Checklist
My suggestion meets these guidelines:
- This wouldn’t be a breaking change in existing TypeScript / JavaScript code
- This wouldn’t change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn’t a runtime feature (e.g. new expression-level syntax)
Issue Analytics
- State:
- Created 5 years ago
- Reactions:5
- Comments:12 (5 by maintainers)
Top Results From Across the Web
Import assignment cannot be used when targeting ...
ts:2:0 Import assignment cannot be used when targeting ECMAScript 2015 modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', ......
Read more >ES5 to ESNext — here's every feature added to JavaScript ...
DOM Event listeners set this to be the target element, and if you rely on this in an event handler, a regular function...
Read more >TSConfig Reference - TypeScript
The target setting changes which JS features are downleveled or left intact. For example, an arrow function () => this will be turned...
Read more >TSConfig Reference - Docs on every TSConfig option
This only affects modules; global script files will not attempt to import modules. For example, with this TypeScript: ts. export function fn( ...
Read more >Module Compiler Option in TypeScript - tsmean
The way different pieces of code could still work together, ... So for example, you'd import jquery in a script tag at the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The
module: hybrid
PR does just thatAny thoughts on my last comment? Essentially, punt
require()
existing to the typings? I guess that should be simpler now thatimport("foo")
types exist, if you candeclare function require(id: string): import(id)
. That would distinguish this frommodule: hybrid
, I think.