-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New rule: no-useless-empty-export #2757
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
Perhaps even an option could be added to make an export required if no imports are present. Maybe this would be even better. rules:
@typescript-eslint/valid-module:
- error
# Removes an empty export if there are other imports or exports. Autofix removes the empty export.
- allowUselessExport: false
# Requires an empty export if there are no other imports or exports. Autofix adds an empty export to the end of the file.
requireModuleSyntax: true |
Could you explain what you mean by this? |
From the TypeScript Handbook Modules page:
I don’t know the exact details of what this means. I do know a specific situation how it impacts my project. Clone and setup Appsemble and open it with a TypeScript editor: git clone https://gitlab.com/appsemble/appsemble.git
cd appsemble
yarn
code . Now open blocks/timer/src/index.ts and blocks/timer/block.ts code blocks/timer/src/index.ts
code blocks/timer/block.ts Everything should be fine. After reading this article I just googled I believe scripts can’t declare modules. Adding any import or export back to the file, will make TypeScript stop complaining. The only logical thing to do is add an empty export. However, this statement is useless as soon as another import or export is added. |
I'm personally of the opinion that you should never Doubly true for declaring modules.
OTOH if you declare a |
Declaring a module is just an example I ran into. The actual issue is about TypeScript behaving differently for modules and scripts. For example the following exports an interface: export interface Foo {} However, the following declares a global interface: interface Foo {} This ESLint rule would avoid the (probably accidental) global interface declaration by autofixing this to: interface Foo {}
export {}; Now the developer using a consuming module will be reminded they forgot to export and import export interface Foo {}
export {}; Now the ESLint rule should complain the |
Note, TypeScript 4.7 is going to allow more control over detection of modules vs scripts: https://devblogs.microsoft.com/typescript/announcing-typescript-4-7-beta/#control-over-module-detection |
Uh oh!
There was an error while loading. Please reload this page.
In TypeScript, modules are TypeScript files that contain an import or export statement. If a file is not a module, it will be ignored by TypeScript. Sometimes a file doesn’t contain imports or exports for legitimate reasons. In this case, an empty export must be added. I.e. to augment an interface:
As soon as the file is modified to contain actual imports or exports, the empty export can be removed.
I’d like to propose a rule that reports empty exports if the file contains other imports / exports. This can be autofixable by removing the empty export. I’m willing to implement this.
The text was updated successfully, but these errors were encountered: