eslint-plugin-import icon indicating copy to clipboard operation
eslint-plugin-import copied to clipboard

TypeScript default overloads treated as multiple default exports

Open AndrewLeedham opened this issue 5 years ago • 4 comments

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

AndrewLeedham avatar Dec 31 '19 17:12 AndrewLeedham

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 avatar Mar 04 '20 15:03 benjie

@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 };

eyedean avatar Mar 22 '20 22:03 eyedean

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.

AverageHelper avatar Jan 19 '21 01:01 AverageHelper

@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)'

azhaochen avatar Jun 13 '22 03:06 azhaochen

Just checking to see if there's an ETA for this fix to land in a release.

jordanbtucker avatar Oct 09 '22 05:10 jordanbtucker

No, there’s no timeline for releases.

ljharb avatar Oct 09 '22 15:10 ljharb

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 avatar May 19 '23 14:05 TomasHubelbauer

@TomasHubelbauer what version of the plugin are you using?

ljharb avatar May 19 '23 15:05 ljharb

[email protected] - I am using it through eslint-config-next 13.4.1

TomasHubelbauer avatar May 19 '23 15:05 TomasHubelbauer

@TomasHubelbauer the fix for this was in v2.27.5, so that's your problem.

ljharb avatar May 19 '23 15:05 ljharb

I see! Thanks!

TomasHubelbauer avatar May 19 '23 15:05 TomasHubelbauer