Eslint-plugin-import: TypeScript default overloads treated as multiple default exports

Created on 31 Dec 2019  路  3Comments  路  Source: benmosher/eslint-plugin-import

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:

bug help wanted typescript

Most helpful comment

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;

All 3 comments

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.

Was this page helpful?
0 / 5 - 0 ratings