eslint-plugin-import
eslint-plugin-import copied to clipboard
TypeScript default overloads treated as multiple default exports
The following example causes 3 error Multiple default exports import/export errors, but there is only technically 1 default export the other 2 are overloads.
export default function foo(param: string): boolean;
export default function foo(param: string, param1: number): boolean;
export default function foo(param: string, param1?: number): boolean {
return param && param1;
}
There seems to be some code for checking overloads: https://github.com/benmosher/eslint-plugin-import/blob/master/src/rules/export.js#L37-L40 but this does not seem to catch this case. I added some debug logs in and the rough contents of namespace is:
root: {
default: [
{
type: 'ExportDefaultDeclaration'
},
{
type: 'ExportDefaultDeclaration'
},
{
type: 'ExportDefaultDeclaration'
}
]
}
I am running this in the following environment:
- eslint: 5.16.0
- eslint-plugin-import: 2.19.1
- eslint-import-resolver-typescript: 2.0.0
- typescript: 3.7.2
- @typescript-eslint/parser: 2.13.0
Related:
- https://github.com/benmosher/eslint-plugin-import/issues/22#issuecomment-569955880
- https://github.com/benmosher/eslint-plugin-import/issues/1357
A workaround is:
function foo(param: string): boolean;
function foo(param: string, param1: number): boolean;
function foo(param: string, param1?: number): boolean {
return param && param1;
}
export default foo;
@benjie sure, that unblocks the case. But, ideally, it should be able to detect it's an overload.
PS. Might be obvious, but just in case for beginners, for non-default exports you similarly do:
function foo(param: string): boolean;
function foo(param: string, param1: number): boolean;
function foo(param: string, param1?: number): boolean {
return param && param1;
}
export { foo };
Can confirm this issue is still present. I too think that the plugin should be able to detect that this case is an overload, and not actually a case of multiple default exports.
@benjie sure, that unblocks the case. But, ideally, it should be able to detect it's an overload.
PS. Might be obvious, but just in case for beginners, for non-default exports you similarly do:
function foo(param: string): boolean; function foo(param: string, param1: number): boolean; function foo(param: string, param1?: number): boolean { return param && param1; } export { foo };
export { foo } should put in the first line, or it will report error 'An export declaration can only be used at the top level of a namespace or module.ts(1233)'
Just checking to see if there's an ETA for this fix to land in a release.
No, there’s no timeline for releases.
export default function test(str: string): void;
export default function test(num: number, str: string): void;
export default function test(arg: string | number, str?: string) {
console.log(arg, str);
}
Output:
Multiple default exports. eslint(import/export)
Looks like this is still present in some form or another. Or is my overload incorrect? There are no errors when pasting the above snippet to the TypeScript Playground verbatim.
@TomasHubelbauer what version of the plugin are you using?
[email protected] - I am using it through eslint-config-next 13.4.1
@TomasHubelbauer the fix for this was in v2.27.5, so that's your problem.
I see! Thanks!