timocov / dts-bundle-generator

A tool to generate a single bundle of dts with types tree-shaking
MIT License
762 stars 40 forks source link

Generics extended with declare module statement aren't included #305

Closed tuomokar closed 6 months ago

tuomokar commented 8 months ago

Bug report

When using the parameter --inline-declare-externals, the produced declare module statement can miss some imports if the statement includes types that extend another type.

Input code

import { RowData } from '@tanstack/react-table';

declare module '@tanstack/table-core' {
    interface TableMeta<TData extends RowData> {
        stickyFilters?: boolean;
    }
}

Expected output

// RowData is imported
import { RowData } from '@tanstack/react-table';

declare module "@tanstack/table-core" {
    interface TableMeta<TData extends RowData> {
        stickyFilters?: boolean;
    }
}

Actual output

// RowData is not imported
declare module "@tanstack/table-core" {
    interface TableMeta<TData extends RowData> {
        stickyFilters?: boolean;
    }
}

Additional context

When building, these errors appear:

node_modules/@tanstack/table-core/build/lib/types.d.ts(17,18): error TS2428: All declarations of 'TableMeta' must have identical type parameters.
ReactTable.d.ts(7,12): error TS2428: All declarations of 'TableMeta' must have identical type parameters.
ReactTable.d.ts(7,36): error TS2304: Cannot find name 'RowData'.

I made a small repository to reproduce the issue. It can be found from here.

Thank you for your time in advance!

timocov commented 7 months ago

A note for myself (or anyone who's concerned) about the issue: currently the tool uses only root file exports to detect whether a type/value is used or not and thus whether it should be added to the bundle (via either way e.g. importing or inlining). With --inline-declare-externals we need to consider all these "declare externals" as well (it might be tricky tho as collecting a list of such externals is done at the same time as processing all files, so we might need to split this into 2 phases).

timocov commented 6 months ago

The fix has been published in 9.4.0 version.