-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Type import statement not erased when exported as default #40420
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
We are experiencing a similiar behaviour: When using an exported interface as Type for a method parameter in combination with a decorator, we get the described warning. The warnings started to popup with typescript version 4.0.2. When using typescript 3.9.4, the warnings did not occur for us. The
The
The used library The warning goes away when
Is this intended behaviour because / as result of the new typed import feature or did it maybe got broken in 4.x versions? You can reproduce the warnings with the zip file attached here : ts-bug.zip
|
Dear @typescript-bot, please do magic with this // @module: es2015
// @showEmit
// @filename: exported.ts
type Foo = number;
export { Foo };
// @filename: index.ts
import { Foo } from "./exported";
export default Foo; |
@janwidmer what you’re describing is a completely different issue. You have
|
@typescript-bot run repros |
Heya @andrewbranch, I've started to run the code sample repros for you. Here's the link to my best guess at the log. |
👋 Hi, I'm the Repro bot. I can help narrow down and track compiler bugs across releases! This comment reflects the current state of the repro in this issue running against the nightly TypeScript. If something changes, I will post a new comment. Comment by @peaBerberian 👍 Compiled import { Foo } from "./exported"; Historical InformationComment by @peaBerberian
|
@andrewbranch I added
When compiling with native
the warning does not pop up. We are also running on a quite old version of Just to make sure I understand you right, in your opinion (with activated We will see how it behaves and might check for duplicates or file a bug after updating our |
Yeah, we don’t have warnings, but it should have an error. The warning you’re seeing from webpack has turned into a build-breaking error in Webpack 5 IIRC. The point of |
@andrewbranch thanks for your detailed explanation, now its's crystal clear :) |
@andrewbranch I guess, your fix #41904 will cover the issue not showing an error in native TS described by me and I don't need to create another bug? (wasn't totally sure if your fix is only for the original problem described in this ticket or for both) |
@janwidmer no, #41904 only fixes the bug described by the OP. |
@andrewbranch I created a bugticket, #42281, as I haven't found any other existing ticket for it. |
TypeScript Version: 4.1.0-dev.20200907
Search Terms: default export type erased
(The keywords I found for that issue are too generic so it may have already been found. Sorry in advance if that is the case.)
Code
I found a strange behavior leading to an error when running the compiled file through node.js. In my [perhaps wrong] opinion, the issue is with how TypeScript compile a TypeScript file to a JS file.
I tried to reduce the issue to the bare minimum. The following code does not anything, but this is the simplification of a problem we encountered in a real bigger codebase.
Let's consider two files:
That's it. Note that there is no importance here that those files only contains types. The same thing happens when real "translate-able" JS code is also there.
The real important part seems to be the
export default
inmain.ts
.Expected behavior:
The expected behavior here is that TypeScript notice that
Foo
is a type and completely erase it from the resulting JS file.When compiling with
tsc --module es2015 main.ts
, I expect to have these two files outputed:And this is exactly what happens if
Foo
is exported as a named export inmain.ts
(nothing is written in those files in typescript <= 3.9.7, an empty export for typescript >= 4.0.2).Actual behavior:
The output from typescript 4.0.2 onward (to 4.1.0-dev.20200907 for my last check) was instead those two files:
So
Foo
does have been erased fromexported.js
but it is kept as an import inmain.js
for no apparent reason. TypeScript still seems to realize that this is a type that should be erased inmain.js
as the export clause was removed.When running the resulting
main.js
through node (I have to in some way communicate to node that those are ES modules before) I receive the following error:While writing this issue, I saw that an
import type
statement exists. If I do aimport type { Foo } from "./exported";
inmain.ts
it does work as expected (main.js
is now only anexport {}
statement).I could stop here and consider this issue as already fixed then.
However, there are two things that makes me think this issue is still one even with that resolution:
import type
statement is defined as useful in conditions I did not meet:if you’ve hit issues under --isolatedModules, TypeScript’s transpileModule API, or Babel, this feature might be relevant.
v3.0.1
) work without any problem on that code, and I didn't use any new feature.Note that I wrote that this is a new issue in TypeScript v4.0.2 onwards but that is not exactly true. The issue exists at least since v3.8.3 (I just know it was not there in v3.0.1) but under another form: in older versions
main.js
still is the same way butexported.js
appears as an empty file. The exact issue I had on my more complex code (strangely) appeared only since updating fromv3.9.7
tov4.0.2
, so I chose to specifically report this issue it for those new versions.The text was updated successfully, but these errors were encountered: