Skip to content

Declaration emit adds unnecessary import types #47849

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

Closed
andrewbranch opened this issue Feb 11, 2022 · 1 comment · Fixed by #48605
Closed

Declaration emit adds unnecessary import types #47849

andrewbranch opened this issue Feb 11, 2022 · 1 comment · Fixed by #48605
Assignees
Labels
Bug A bug in TypeScript Domain: Declaration Emit The issue relates to the emission of d.ts files Fix Available A PR has been opened for this issue

Comments

@andrewbranch
Copy link
Member

andrewbranch commented Feb 11, 2022

Two low-hanging fruits for improvement:

  1. Reusing type annotations on optional parameters:
    // a.ts
    export interface Foo {}
    declare global {
      namespace ns {
        export { Foo }
      }
    }
    
    // b.ts
    type Foo = ns.Foo;
    const bar = (foo?: Foo) => {};
    Declaration emit would use the parameter type Foo as written if the parameter weren’t optional, and if bar were written as a function declaration. When it’s optional, under strictNullChecks, it creates a new type identity unioned with undefined and prints that new type, so it doesn’t realize Foo is locally accessible, and it doesn’t randomly trawl global namespaces so it would never come up with ns.Foo, so instead it writes import("./a").Foo. Allegedly, even without strictNullChecks, it still starts going through this route and doesn’t realize that we should just be able to print the type as written.
  2. An existing workaround is to replace type with import. So declaration emit is looking to see if aliases are in scope, but not type aliases or other local declarations.
@andrewbranch andrewbranch self-assigned this Feb 11, 2022
@andrewbranch andrewbranch added the Bug A bug in TypeScript label Feb 11, 2022
@andrewbranch andrewbranch added this to the TypeScript 4.7.0 milestone Feb 11, 2022
@DanielRosenwasser DanielRosenwasser added the Domain: Declaration Emit The issue relates to the emission of d.ts files label Feb 11, 2022
@andrewbranch
Copy link
Member Author

Allegedly, even without strictNullChecks, it still starts going through this route and doesn’t realize that we should just be able to print the type as written.

Update: this is false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: Declaration Emit The issue relates to the emission of d.ts files Fix Available A PR has been opened for this issue
Projects
None yet
3 participants