pzavolinsky / ts-unused-exports

ts-unused-exports finds unused exported symbols in your Typescript project

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

question about import * and default

mrseanryan opened this issue · comments

Question: currently, import * with default, seems to always mark the default as unused.

Unit test:

Scenario: Import * marks 'default' as unused
  Given file "a.ts" is
    """
    export const a = 1;
    export default 1;
    """
  And file "b.ts" is import * as all from './a';
  When analyzing "tsconfig.json"
  Then the result at a.ts is ["default"]

But in this case, default could still be used, as all.default

is that something we should check for?

(could be tricky, as it requires processing statements of the importing file)

I'm not really sure the all.default is actually part of the spec.

It might be a detail of how modules are implemented these days but that might change. I've taken a look at this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import and there is no mention of that case.

I'd say we should wait implementing this until someone requests it.

In my mind, the most common use case for *:

import * as all from './a';`

means "everything but the default export", and if I really want everything I'd do:

import a, * as all from './a';`

The risk of assuming (even if globally) that all implies default is too many false positives and doing analysis of each usage is probable super complex (not warranted for a corner case such as this one).

IMO, if the only usage of your default symbol is through this hack you should probably refactor this into a named export anyway.

ok - thanks for the answer