sindresorhus / eslint-plugin-unicorn

More than 100 powerful ESLint rules
MIT License
4.2k stars 363 forks source link

`prefer-export-from` breaks TypeScript support for `import *` #1617

Open KurtPreston opened 2 years ago

KurtPreston commented 2 years ago

I have the following TypeScript code:

import * as $MyJsonFile from './myJsonFile.json';
export const MyJsonFile = $MyJsonFile;

The prefer-export-from rule wants me to change the line to

export * as MyJsonFile from './myJsonFile.json';

Unfortunately, this results in the TypeScript compilation error:

Module '"myJsonFile"' uses 'export =' and cannot be used with 'export *'.ts(2498)

Ideally, prefer-export-from would be able to ignore export = modules which are incompatible with the syntax. If that is not possible to infer, then it would be nice to be able to specify certain patterns to exclude from the rule, like:

"prefer-export-from": ["error", {
  "ignorePatterns": ["*.json"]
}]
fisker commented 2 years ago

Does export {default as MyJsonFile} from './myJsonFile.json'; work?

KurtPreston commented 2 years ago

@fisker Unfortunately not. It results in this error:

Module '"./myJsonFile.json"' has no exported member 'default'.ts(2305)
fisker commented 2 years ago

Is this a typescript issue?

fregante commented 1 month ago

I can confirm that this is a TypeScript issue.

A few notes:

Given that unicorn produces valid code, this isn't a bug here. I'm reclassifying it as a feature request to improve support with TS until TS fixes their own bug.

If you file a report with TS or find an open bug, please link it here.