webpro-nl / knip

✂️ Find unused files, dependencies and exports in your JavaScript and TypeScript projects. Knip it before you ship it!
https://knip.dev
ISC License
6.57k stars 151 forks source link

Packages re-exported under different name through typescript module declaration #746

Open allista opened 1 month ago

allista commented 1 month ago

Reproduction url

https://codesandbox.io/p/devbox/gq2qss?file=%2Fremotes.d.ts

Description of the issue

We're using module federation and import from remote containers at run time. To provide type safety, we use types from the packages that provide remote containers, and re-export those types through typescript module declaration, so that the types match the names of federated containers as defined in federation configuration:

// This module name is used in module federation configuration
declare module "@my/remote-container" {
  // This is a type-only re-export from a package that
  // provides a remote container from module federation
  export * from "actual-remote-container-package";
}

In another file:

// This import works in real setup only at runtime,
// because it refers to a remote container from module federation
import {SomeThing} from '@my/remote-container';

SomeThing.foo();

With this setup knip reports both the package used to export types from under declare module as "Unused" and the import from the declared module as "Unlisted":

> knip

Unused devDependencies (1)
actual-remote-container-package  package.json
Unlisted dependencies (1)
@my/remote-container  index.ts
webpro commented 1 month ago

Thanks @allista, this is indeed an yet unsupported feature.

FYI, at this point I'm not implementing new features so for now you could use the ignore* options (which can be regexes if that's useful).