Skip to content

Declaration emit adds unnecessary import types #47849

Closed
@andrewbranch

Description

@andrewbranch

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.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDomain: Declaration EmitThe issue relates to the emission of d.ts filesFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions