TypeScript: import * as alias syntax doesn't work with export = function unless merged with namespace
declare module "foo" {
function foo(): void;
export = foo;
}
Can’t be included via import * as f from "foo";.
(test.ts(1,20): error TS2497: Module '"foo"' resolves to a non-module entity and cannot be imported using this construct.)
Whereas
declare module "foo" {
function foo(): void;
namespace foo {}
export = foo;
}
Can be. The distinction seems artificial.
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 22
- Comments: 17 (12 by maintainers)
Commits related to this issue
- add empty namespace declaration for chai-as-promised to allow es6 imports See https://github.com/Microsoft/TypeScript/issues/5073 and #6697 — committed to sventschui/DefinitelyTyped by deleted user 8 years ago
- add empty namespace declaration for chai-as-promised to allow es6 imports See https://github.com/Microsoft/TypeScript/issues/5073 and #6697 — committed to borislavjivkov/DefinitelyTyped by deleted user 8 years ago
- added an empty namespace in koa-bodyparser.d.ts Without this, there will be `Module '"koa-bodyparser"' resolves to a non-module entity and cannot be imported using this construct. (2497)` error This... — committed to liushigit/DefinitelyTyped by liushigit 8 years ago
- added an empty namespace in koa-bodyparser.d.ts (#8979) Without this, there will be `Module '"koa-bodyparser"' resolves to a non-module entity and cannot be imported using this construct. (2497)` err... — committed to DefinitelyTyped/DefinitelyTyped by liushigit 8 years ago
- added an empty namespace in koa-bodyparser.d.ts (#8979) Without this, there will be `Module '"koa-bodyparser"' resolves to a non-module entity and cannot be imported using this construct. (2497)` err... — committed to DefinitelyTyped/DefinitelyTyped by liushigit 8 years ago
- Missing locale functions and types (#8981) * added svgRendering property to Html2CanvasOptions interface * added an empty namespace in koa-bodyparser.d.ts (#8979) Without this, there will be `M... — committed to DefinitelyTyped/DefinitelyTyped by davhdavh 8 years ago
- Allow ES6 import syntax for wolfy87-eventemitter.d.ts See https://github.com/Microsoft/TypeScript/issues/5073 — committed to tkrotoff/DefinitelyTyped by tkrotoff 8 years ago
- Allow ES6 import syntax for wolfy87-eventemitter.d.ts (#9415) * Boostrap -> Bootstrap * Allow ES6 import syntax for wolfy87-eventemitter.d.ts See https://github.com/Microsoft/TypeScript/issues/... — committed to DefinitelyTyped/DefinitelyTyped by tkrotoff 8 years ago
- Fix error TS2497 on `import * as X from 'statsd-client'`: Per Microsoft/TypeScript#5073, closed as `By Design` by @mhegazy, we need to merge a namespace with the export when using `export =`, otherwi... — committed to garthk/DefinitelyTyped by garthk 8 years ago
- Fix error TS2497 on import * as X from 'statsd-client': Per Microsoft/TypeScript#5073, closed as `By Design` by @mhegazy, we need to export a namespace for `import *` to work, else `TS2497`. That cla... — committed to garthk/DefinitelyTyped by garthk 8 years ago
- Fix error TS2497 on import * as X from 'statsd-client': Per Microsoft/TypeScript#5073, closed as `By Design` by @mhegazy, we need to export a namespace for `import *` to work, else `TS2497`. That cla... — committed to garthk/DefinitelyTyped by garthk 8 years ago
- Fix error TS2497 on import * as X from 'statsd-client': Per Microsoft/TypeScript#5073, closed as `By Design` by @mhegazy, we need to export a namespace for `import *` to work, else `TS2497`. That cla... — committed to garthk/DefinitelyTyped by garthk 8 years ago
- Fixing react-addons-shallow-compare Seems like in TypeScript 2.0 this is still present https://github.com/Microsoft/TypeScript/issues/5073. — committed to ahz/DefinitelyTyped by deleted user 8 years ago
- More typescript definition work. https://github.com/Microsoft/TypeScript/issues/5073 — committed to convergencelabs/string-change-detector by mmacfadden 7 years ago
- added empty namespace because of https://github.com/Microsoft/TypeScript/issues/5073 — committed to spectralradius/websocket-json-stream by spectralradius 7 years ago
- Update index.d.ts Improving export (https://github.com/Microsoft/TypeScript/issues/5073) — committed to gnain/DefinitelyTyped by gnain 7 years ago
- fix(typings): fix type for angular 5 in issue #112, we saw the problem with angular 5. in my tests, i install angular 5 and rollback the definition without workarround solved for default function ht... — committed to claytonsilva/cep-promise by claytonsilva 6 years ago
- Prefer a namespace over declare module Apparently import * as alias syntax does not work with export = function: https://github.com/Microsoft/TypeScript/issues/5073 — committed to ljani/tslint-webpack-plugin by ljani 6 years ago
- Typings: Prefer a namespace over declare module (#13) * Prefer a namespace over declare module Apparently import * as alias syntax does not work with export = function: https://github.com/Micros... — committed to jrparish/tslint-webpack-plugin by ljani 6 years ago
With
esModuleInteropavailable, you should not need this hack anymore. You should be able to do:@mhegazy To follow up on that, the
namespace {}hack proposed and used in every linked issue here enables the behaviour you describe. It seems there’s enough people committing the hack (and having it merged) that it would warrant looking at whether you should enableimport x = require('x'). If not, perhaps those PRs should start being rejected with proper guidance on how to use the import syntax in these cases (E.g. by recommending people use CommonJS modules instead of ES6 or just informing people ofimport x = require('x')- I’ve found enough people just didn’t know theimport x =style existed it or have tried to enforce a style without understanding the implications and then use this hack to make things work instead).So, is this the hack? And is it still the best way?
there is no way in an ES6 module to achieve this. the parallel in an ES6 module world is to
defaultexport.import *will import the namespace component of the export, if it exists, if it does not, it is an error. That you can call the alias as a function in the second case, is the bug i would say.Without the hack, TS code cannot target es6 if it consumes cjs module.
With Babel, JS user is doing
import x from 'x'and it is working fine for them. For TS, right now we do not have any working solution except the hack.@mhegazy I agree, I’m only proposing that there be an experimental flag that enabled emitting CommonJS/AMD/etc imports when using ES6 modules. This would enable whoever is trying to use ES6 modules today to use CommonJS modules without the
namespace X {}hack.The request wasn’t about the other direction of supporting ES6 imports of CommonJS, I know that’s an issue and already something the TypeScript team will likely need to change with the latest node proposals. This proposal was only for using the legacy
import x = require('x')syntax when targeting ES6 modules. If a user has enabled this proposal, it’s hopeful that they know what they’re doing. If it’s decided by the TypeScript team that this feature isn’t needed, perhaps thenamespace {}hacks should stop being merged which enable the behaviour you describe (just look at the linked issues to this one).Reference on current node proposal - https://github.com/nodejs/node-eps/pull/39. Looks like the current way of typing and importing CommonJS modules may be invalid using this proposal.
@mhegazy Have you considered enabling a flag for CommonJS interop in ES6 modules? It’s inevitable that people will want to use CommonJS requires and have ES6 exports for a period of time.
Edit: To clarify, in case that didn’t make sense, a flag that allows
import x = require('x')syntax when using ES6 exports. Having every node module update now, or into the future, seems pretty unfeasible so even people that will adopt ES6 (now with their bundler or in the future with native support) will require to continue usingimport x = require('x')for “legacy” external modules.