Description
(my original comment on the ES6 modules thread: #2242 (comment))
Here's a challenge: ask the average programmer what this soon-to-be-valid TypeScript code does:
import X from "M";
My guess is that 90% of the respondents will tell you it would probably import some member (or "export", in the official terminology) called X
from a module called M
. Now what about this?
import X, * as Z from "M";
Well, that would probably import member X and the whole module (represented by *
) aliased as Z from M
. Then show them this:
import {X} from "M";
And this:
import {X, Y as Z} from "M";
My guess? they'll answer "the same?" - meaning it imports X
as itself and Y
with alias Z
Now here's the real thing: according to the spec, the first example, although appearing quite straightforward, would do something _completely different_ than most people (including myself) would expect. It wouldn't import an export called X
but instead _alias the default export_ of M
to the identifier X
.
So essentially it would appear to compile and run but later give out unexpected and weird run-time errors when X
is accessed and used with a different semantic meaning. The third example, on the other hand, would import the X
member as expected.
The second and forth examples, although very similar in appearance, still suffer from this problem, In the second example X
will alias the _default_ export, but in the forth it would import the member X
as expected.
So the first example could have been replaced, for instance, by a simpler and more natural syntax such as this:
import "M" as X;
And the second (not sure if any of this is valid syntax today but certainly could be):
import default as X, * as Z from "M";
// or
import {default as X, * as Z} from "M";
This may not be a TypeScript issue per se, but I cannot understand how anyone would allow such a poorly designed syntax to be introduced to an existing language that's currently quite neat and readable? I believe that as soon as this syntax gets to wider adoption, it would be inevitable to change it. ES would be literally _forced_ to fix this by the masses of developers perplexed by this insanity.
I personally think it's better sooner than later, and now this gaining early adoption within TypeScript and other transpilers, there's going to be actual production code using this, so a price may have to be payed when this is "fixed" later by ES.
Please keep this issue open as I honestly consider this to be a bug. It wouldn't be wise to direct all responsibility to ES as there are some talented people at Microsoft who have been spending years now in designing, developing and implementing a scalable, usable and readable language. It would be demotivating to force them to maintain and support a faulty and confusing syntax such as this (and later spend much time and thought on how to fix it and work around legacy code still making use of the "older" syntax).