-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Consider emitting export {}
in all ambiguous module output
#38696
Comments
This breaks code.. |
@WORMSS Is your problem the one I've just raised with webpack? (webpack/webpack#11536) |
@RoystonS no. I was not trying to export anything. I wasn't even trying to do a 'code' import.. just a 'type' import. So it seems import type { SomeType } from 'SomePackage';
const a: SomeType = {}; is enough to class the whole file as a file that requires a module.. const a = {}; now the output is const a = {};
export {}; This means you cannot link directly to the compiled JS file to run synchronously in a web browser, or run the file in nodejs in classic mode My work around has been to just manually go into the compiled files and delete |
May I know what the workaround to the problem raised by @WORMSS above? All our code breaks and we can no longer use the transpiled js file directly in the browser. We are not using webpack whatsoever. Is there any setting in tsconfig that can prevent this new behaviour? |
Apparently if you use |
Yeah, this is unexpected behavior. If a person wants |
Still not sure why this is occurring I am simply importing an interface from a file and it is now emitting export{} because the file contains both the interface and its implementation. |
I tried to it search it from release note and I found this thread. I have an issue with older version of typescript that, when e.g. // foo.ts
export interface foo{
bar:string
}
// export {}; // bar.ts
import type { foo } from './foo'
const bar: foo = { bar:'foobar'}; 👆 It failed on typescript 3.8 And then I spotted the behavior change since TS 4.0 Question. |
I am not the project maintainer. But as I understand it, this is the new desired behavior. So feel free to upgrade. It is possible a configuration flag will be added in tsconfig.json for those who want the old behavior though. |
Do we know if its possible to do a post typescript process hook or something? so we can do fileContent.replace('export {}', ''); or something similar to get rid of this stupid behavior? |
In TypeScript 3.9, we started preserving
export *
for type-only modules. While this keeps our behavior more predictable long-term, this caused bundle-size regressions for teams combining Webpack and TypeScript (webpack/webpack#10889).My understanding of the situation is that tools like Webpack use a similar disambiguation strategy as that of TypeScript to figure out whether a file is a module. Given that, when Webpack resolved to an empty file, it would bail out of certain optimizations thinking it was a global script file. In reality, all users using ts-loader's
transpileModule
mode were hitting this as well - it just turned out that the new behavior uncovered this behavior in a very large codebase.We also discovered that we were not emitting
"use strict"
prologue directives even underalwaysStrict
because we assumed that the output would be a module.I also just discovered that
import type { foo } from "bar"
doesn't force anexport {}
in a module that does have values in it. That seems bad because this goes beyond special-casing for empty modules.Here's the outcomes I could imagine:
export {}
. Is this really useful if the fix is already in-flight for Webpack?export {}
underalwaysStrict
when the output is empty (but then why does this matter?)Personally, my gut was to always emit the
export {}
; then I swung into thinking that emittingexport {}
on empty modules would be a weird vestigial behavior for empty modules. But then I realized that TypeScript doesn't emit any sort of module marker even when usingimport type
, so I'm back to thinking we should always emitexport {}
.The text was updated successfully, but these errors were encountered: