question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Import assignment should work with esnext targets

See original GitHub issue

Search 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:open
  • Created 5 years ago
  • Reactions:5
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
weswighamcommented, Jul 10, 2019

The module: hybrid PR does just that

0reactions
simonbuchancommented, Jul 29, 2019

Any thoughts on my last comment? Essentially, punt require() existing to the typings? I guess that should be simpler now that import("foo") types exist, if you can declare function require(id: string): import(id). That would distinguish this from module: hybrid, I think.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found