-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Inconsistent import behavior would cause .d.ts
to break.
#7398
Comments
In similar context the
The term "external module format" is not exact (and not align with TypeScript to my understanding). But that's YET another topic. 🌹 |
cc @blakeembrey |
@unional can you provide an example of the issue. i am having hard time understand the underlying problem. |
Say I have a commonjs // foo module
module.export = function foo() {} I can import // boo module
import foo from 'foo';
import * as foo2 from 'foo'; Currently, depends on the value of So the problem is this. If I distribute the i.e. when an The problem cannot be solved in Does this help? |
thanks, this is helpful. this is correct. the problem here is not CommonJS has a concept of ES6 module loader implementation, decided to add a shim for these, using the default export. so for instance, es6-module-loader, used by Systemjs, will have the module exports all under the keep in mind that other loaders, i.e. commonjs has no idea of a So in short, how you can consume the module depends on what loader you are using.
if your loader is not doing this copying, do not enable this flag. does this cover the OP? |
Agree, and it actually helps to narrow the gap. My concern in this post is not the usage of As you said "the problem is the loaders", the consequence it that if any package author starts to:
And then the consumer use it in Or in other combinations that the package author's intent of Would |
I am not sure i see what you mean here. Either the package author has a default export or they do not. if they do, then this discussion is not relevant, if they do not, the importer needs to decide how to import it, either they import using
I do not see how UMD would solve this issue. |
It's not what they export, it's what they import. i.e. // moduleA
module.export = foo() {} // moduleB
import foo from 'moduleA'
// or `import * as foo from 'moduleA'`,
// depends on moduleB's `module` and `allowSynthethicDefaultImports`
export default function boo() { foo(); }
export foo; // re-export or extends it. // appC
import boo from 'moduleB'
// do something If UPDATED sample |
i see. |
Honestly, don't know~~~ 😜 What I can think of are these:
Especially 1. But I don't know if that is possible because it involves multiple loaders. |
Sorry, could you clarify what problem you will have in |
The resolution will get the wrong thing. E.g. Thus the types used in |
Same thing (or worse?) applies if the module is distributed in |
This is not exactly a TypeScript issue. It is really a issue of the interop between ES6 and commonjs. It is just that the effect impact TypeScript first because of the distribution of the declaration files. When people start to distribute package in raw ES6 and it consumes nodeJS package...... .....it sounds like a systematic crisis. :( I hope I'm wrong. Maybe you can invite members of the loaders and ES6 to discuss this? |
Just to complete the story (as you know very well already), not only the ES6 syntax is affected: // module = "commonjs", allowSynthethicDefaultImports = true | false
import tape = require('blue-tape');
isFunction(tape);
isUndefined(tape.default);
// module = "system"
import tape = require('blue-tape');
!isFunction(tape);
isImmutableObject(tape);
isFunction(tape.default); |
Perhaps the correct thing to do is to not respect |
However, the above case is still inconsistent no matter what the |
Also, non-ambient (source code) also has this issue if they are distributed. Although currently it is rare. |
What do you think about the module directive proposal?
|
By the way, (2) and (3) doesn't work with https://github.com/typings/typings as there is no |
I'm seeing this starts to take effect in live code, not just typings files. The codebase starts to divide. For example, currently, in order to use import * as createLogger from 'redux-logger' instead of import createLogger from 'redux-logger' as suggested in its document: https://github.com/evgenyrodionov/redux-logger Although the source of If I use |
Update for TC-39: |
TL;DR;
The current inconsistent import behavior in different module mode (
system
orcommonjs
) would cause.d.ts
to break (1.8 included).Potential Solution
.d.ts
file will be processed correctly.d.ts
uses a module agnostic import/export syntaxContext
The context of this problem is about module mapping (such as what
jspm
andtypings
supports).Basically saying any module can be referenced by a different name to avoid module name collision. e.g. in a project that use two versions of
jquery
, one would be imported normally (import $ from 'jquery'
), one would be imported with a different name (import $old from '[email protected]'
).In
1.8
, with the support ofallowSyntheticDefaultImports
enables the behavior become similar (but if the user set it tofalse
could still diverge). However the behavior ofimport * as A from 'abc';
andimport dr = require('domready');
are still different.Problem
The current (1.8)
module
support is working fine when creating package in TypeScript and distributing it as.js
.However it does not work if the package is distributed in
.ts
or with.d.ts
.The reason is that when distributing in the compiled form, the
module
mode used by the package author is captured in the.js
file. When distributing.ts
and.d.ts
it relies on the consumer to use the samemodule
mode as the package author intended.This means if the package authors (including all typings authors) create the package using one
module
mode, and the consuming application/package use a differentmodule
mode, the system will fail.It is worse if the consuming application/package uses multiple packages with disagreeing
module
mode.Here are the original discussion:
typings/typings#149
This is closely related to:
#7125
Please let me know if there are any confusion or missing information / context.
The text was updated successfully, but these errors were encountered: