Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Statements aren't inlined if they are used in declare global statements only #214

Closed
ext opened this issue Jul 15, 2022 · 4 comments · Fixed by #246
Closed

Statements aren't inlined if they are used in declare global statements only #214

ext opened this issue Jul 15, 2022 · 4 comments · Fixed by #246
Assignees
Labels
Milestone

Comments

@ext
Copy link

ext commented Jul 15, 2022

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.

@ext
Copy link
Author

ext commented Jul 15, 2022

Minimal repo showing the issue: https://github.com/ext/dts-bundle-generator-issue-214

@timocov timocov added the Bug label Jul 17, 2022
timocov added a commit that referenced this issue Jul 17, 2022
@timocov
Copy link
Owner

timocov commented Jul 17, 2022

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.

@timocov timocov changed the title Imports for global namespaces is stripped Statements aren't inlined if they are used in declare global statements only Jul 17, 2022
@timocov timocov added this to the 8.0 milestone Mar 30, 2023
@timocov timocov self-assigned this Mar 30, 2023
timocov added a commit that referenced this issue Mar 30, 2023
@timocov
Copy link
Owner

timocov commented Mar 30, 2023

Released in v8.0.0.

@ext
Copy link
Author

ext commented Mar 30, 2023

Awesome, thank you very much for implementing this! ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants