dts-bundle-generator
dts-bundle-generator copied to clipboard
Statements aren't inlined if they are used in `declare global` statements only
Bug report
Declared global namespaces is stripped or does not contain the necessary imports.
Not sure if I just cant get the options right of if there is something else wrong.
Input code
import type { FooOptions } from "./options";
declare global {
export namespace Cypress {
export interface Chainable {
foo(options?: FooOptions): void;
}
}
}
(The contents on FooOptions
is irrelevant, even an empty interface export interface FooOptions {}
causes the problem)
Expected output
Above code intact in the output file.
Actual output
dts-bundle-generator src/foo.ts -o dist/foo.d.ts
Without any flags I get an empty export:
// Generated by dts-bundle-generator v6.12.0
export {};
dts-bundle-generator --inline-declare-global src/foo.ts -o dist/foo.d.ts
With --inline-declare-global
I get the global namespace in the output file but without the FooOptions
import:
// Generated by dts-bundle-generator v6.12.0
declare global {
export namespace Cypress {
interface Chainable {
foo(options?: FooOptions): void;
}
}
}
export {};
This is also detected when dts-bundle-generator
checks the result:
> dts-bundle-generator --inline-declare-global src/foo.ts -o dist/foo.d.ts Compiling input files... Processing src/foo.ts Writing src/foo.ts -> dist/foo.d.ts Checking generated files... dist/foo.d.ts(6,18): error TS2552: Cannot find name 'FooOptions'. Did you mean 'FocusOptions'? Error: Compiled with errors
Additional context
Manually inlining the types in the same file works as expected but is not really viable in a real world scenario (more complex types being shared between multiple files)
-import type { FooOptions } from "./options";
+export interface FooOptions {
+ name: string;
+}
declare global {
export namespace Cypress {
export interface Chainable {
foo(options?: FooOptions): void;
}
}
}
For context, this happens when I'm trying to use dts-bundle-generator
on a Cypress plugin using the technique highlighted in their documentation: Types for Custom Commands:
Thanks for writing and maintaining this tool, I find it really useful and easier to use than alternatives.
Minimal repo showing the issue: https://github.com/ext/dts-bundle-generator-issue-214
Hi @ext,
Thanks for reporting this. It looks like the tool doesn't take into consideration for "whether a type is exported" global types that you're going to include into a bundle.
The bug is that the tool works in the way that it gets the list of export statements from an entry point and uses it as a "basis" to check "whether a statement is exported or is used any any root export indirectly via any chain is usages". Obviously global declaration statements aren't part of this check and that's why FooOptioins
in your repro isn't inlined.
It might be tricky though to get it fixed, because afaik there is no compiler API to get all "global" statements (or especially all global statements that we declared in the program). We can try to collect these statements manually, but we need to keep in mind that declare global
statement might be part of a source file, part of another module declaration declare module
, it might include nested statements, namespaces and modules (and most likely I forgot about something) - all this should be handled correctly to make it work.
For these of you who reads this and who would like to take a took into the issue, debug it or provide a fix, I created a branch with adding a test case (created by @ext - thank you for that!) - https://github.com/timocov/dts-bundle-generator/tree/issue214.
Released in v8.0.0.
Awesome, thank you very much for implementing this! :heart: