Skip to content

Export Type { Interface } creates empty export #47470

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

Open
Caleb-T-Owens opened this issue Jan 16, 2022 · 4 comments
Open

Export Type { Interface } creates empty export #47470

Caleb-T-Owens opened this issue Jan 16, 2022 · 4 comments
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@Caleb-T-Owens
Copy link

Caleb-T-Owens commented Jan 16, 2022

Bug Report

🔎 Search Terms

export type
export type interface
export type creates empty export

🕗 Version & Regression Information

From version 4.0 till nightly

The issue that I'm discovering is that my JS output is being covered in files containting export {} where I've defined a interface that gets exported. It would be preferable if these files could not be created.

⏯ Playground Link

Playground link with relevant code

💻 Code

interface Foo {

}

export type { Foo }

or

export type Foo = string

🙁 Actual behavior

Generates a JS file with an empty export, IE

export {}

🙂 Expected behavior

I would expect there to be no JS file generated as types are only used at compile time.

There really shouldn't be any imports for the interfaces left in the JS so having these loose exports tidied up shouldn't cause any unexpected side effects.

@MartinJohns
Copy link
Contributor

MartinJohns commented Jan 16, 2022

This is working as intended. As soon as you use import or export you're dealing with a module, and compiled modules receive this export statement (to make the compiled file a module as well). See #41513 for details.

@fatcerberus
Copy link

w.r.t. the explanation given in #41513 - For what it's worth, per ECMAScript, whether a .js file is a module or not is entirely a function of how the runtime chooses to load it; this is why we have <script type="module"> in HTML and "type": "module" in Node. Leaving an empty export {} in the output file to "preserve the moduleness of the file" is unnecessary; to my knowledge, no runtime uses the presence of import/export as an indicator of whether to load a file as a module or not, that's strictly a TypeScript thing.

@Caleb-T-Owens
Copy link
Author

Caleb-T-Owens commented Jan 17, 2022

So, after reading the #41513, it seems that the intention is to represent that the given file is a module, is this not the .mjs file extention for this purpouse. The .mjs extension does have the unfortunate side effect of some servers disagreeing with MIME types but sounds like this is generally being fixed, so would it be possible to add a compiler switch to change from generating export {} to either nothing or .mjs extensions?

@RyanCavanaugh RyanCavanaugh added In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Jan 20, 2022
@RyanCavanaugh
Copy link
Member

Technically a duplicate but we've recently been discussing removing the export { } output file marker for the reasons @fatcerberus outlined above

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants