-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Import assignment not allowed when module format is ES modules #22321
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
Comments
Maybe I've missed something, but can you use |
Maybe I'm missing something: I can't use an |
Maybe it's confusing because I didn't include an I want this: import * as foo from './foo.js';
import Koa = require('koa'); to output: import * as foo from './foo.js';
const Koa = require('koa'); |
Also, |
Ah, duh, right. |
But in that case, your loader ( |
besides I have a lib // upper_lib
class Upper {}
export = Upper
// my_lib
import Upper = require('upper_lib'); // I have to use import assign because upper_lib is 'export = Upper'
export class MyClass extends Upper {} But I want // tsconfig.json
"module": "esnext"
// my_lib
import Upper = require('upper_lib');
// report error: Import assignment cannot be used when targeting ECMAScript modules. // tsconfig.json
"module": "esnext"
// my_lib
import * as Upper from 'upper_lib';
// report error: Module 'upper_lib' resolves to a non-module entity and cannot be imported using this construct. // tsconfig.json
"module": "commonjs"
// my_lib
import Upper = require('upper_lib');
// OK, but not what I want - no tree shaking. // tsconfig.json
"module": "commonjs",
"esModuleInterop": true,
// my_lib
import Upper from 'upper_lib';
// OK, but not what I want - no tree shaking. // tsconfig.json
"module": "esnext",
"esModuleInterop": true,
// my_lib
import Upper from 'upper_lib';
// report error: Module 'upper_lib' has no default export. // tsconfig.json
"module": "esnext",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
// my_lib
import Upper from 'upper_lib';
// OK, but other project depends 'my_lib' need to set then same tsconfig |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
@DanielRosenwasser I think this issue is still relevant, especially as module interop is defined on the node side of things. Basically all the Definitely Typed typings assume that you can have named exports from CJS modules, which 1) doesn't work if you have to require() CJS and 2) doesn't work if CJS doesn't have named exports, but default exports the So I think import assignment should work in module output, so you can |
as someone who doesn't really know much about any of this CJS vs ES Modules nonsense, I've arrived here after enabling is there any update, 6 months later, on the proper way to import a package that uses statements like again, I don't really have a clear and full understanding of all the complexities, I'm just trying to use a library. for context/ an example: import * as Autosuggest from 'react-autosuggest' // what I had before `esModuleInterop`, which was great
import Autosuggest = require('react-autosuggest') // complains that this doesn't work with EMCAScript modules as a target
const Autosuggest = require('react-autosuggest') // works, but types are not imported, so lots of red lines appear
// @ts-ignore
import Autosuggest = require('react-autosuggest') // not too surprisingly, fails at runtime |
@ekilah with import Autosuggest from 'react-autosuggest' |
@DanielRosenwasser sorry, I left that case out. Still doesn't work: import Autosuggest from 'react-autosuggest' // TS1129: Module '".../node_modules/@types/react-autosuggest/index"' has no default export
import {Autosuggest} from 'react-autosuggest' // TS2305: Module '".../node_modules/@types/react-autosuggest/index"' has no exported member 'Autosuggest'. and here's the top of the types file for reference: import * as React from 'react';
declare class Autosuggest<T = any> extends React.Component<Autosuggest.AutosuggestProps<T>> {}
export = Autosuggest;
declare namespace Autosuggest { ... } |
TypeScript Version: 2.7.2
Search Terms: import assignment modules import = require types
Code
When targeting CommonJS, I can pull in values and types from a CommonJS module like so:
Which generates a normal require statement:
Actual behavior:
When targeting MS modules, import assignment triggers the warning:
The suggestions aren't available though, because
module.exports =
style export.import
statement will emit andimport
statement, and I need it to emitrequire()
to use with@std/esm
(more on this latter).The only way to generate a
require()
is to use one without import assignment, but then we have to jump through major hoops to import the types:This is obviously pretty cumbersome.
Expected behavior:
I think supporting import assignment when emitting ES modules is the easiest solution. Just continue to emit:
Obviously, we don't know how CJS/ESM interop is going to behave quite yet. The possibilities range from:
import
statements (probably unlikely due to Node.js team preferences)module.exports
(most likely)Regardless of the CJS interop support, it will at least possible to still use
require()
(maybe still a global, maybe viaimport.meta.require
).But even with interop, using
require()
is necessary for type-checking because existing typings describe what's returned byrequire()
, not what would be returned from the CJS interop proposals. That is, existing typings do not describe an ES module with a default export. TypeScript may also need a way to transform typings under various interop schemes, but that's a separate issue.So, currently at least, we need to emit
require()
insteadimport
and also bring in the types. Import assignment does exactly this.Another option could be to offer an easier way to import just the type of a module and cast the
require()
result, but that seems to only more verbosely describe what import assignment already does. Essentially it would be allowing this:Without the error that's generated from the
import
:Playground Link: BTW, there's no option in the playground to set the "module" compiler option.
Related Issues:
#19500
The text was updated successfully, but these errors were encountered: