Skip to content
This repository was archived by the owner on Sep 1, 2026. It is now read-only.
This repository was archived by the owner on Sep 1, 2026. It is now read-only.

Multiple differences in declaration emit for CommonJS exports #2533

Closed

Description

The following declaration emit issues were noticed in #2513:

  • Use of typeof module.exports.xxx (which isn't permitted in .ts files),
  • Emitting multiple export declare var foo: Foo for multiple assignments to exports.foo, and
  • Emitting export declare var foo: Foo instead of export const foo: Foo for CommonJS exports.

Issues are demonstrated by several tests, including:

tests/cases/conformance/jsdoc/declarations/jsDeclarationsExportDefinePropertyEmit.ts
tests/cases/conassignmentToVoidZero1.tsformance/salsa/
tests/cases/conformance/salsa/moduleExportDuplicateAlias.ts

Activity

  1. added
    bug
    Domain: Declaration Emit
    on Jan 16, 2026
  2. RyanCavanaugh commented on Apr 14, 2026

    @RyanCavanaugh
    Member

    Confirmed all three still exist in nightly

  3. ahejlsberg commented on Apr 14, 2026

    @ahejlsberg
    MemberAuthor

    Note that we support multiple assignments to module.exports and exports.XXX, so in declaration emit we should emit only for the first occurrence. That would also eliminate the odd duplicate declarations you'd otherwise see.

  4. ahejlsberg commented on Apr 14, 2026

    @ahejlsberg
    MemberAuthor

    There are other JS declaration emit issues. For this JS file:

    export class C {
      constructor() {
        this.x = 1
        this.y = 2
      }
    }
    
    export const obj = {}
    obj.x = 1
    obj.y = 2
    
    export function f() {}
    f.x = 1
    f.y = 2

    We generate this declaration file:

    export declare class C {
        constructor();
    }
    export declare const obj: {
        x: number;
        y: number;
    };
    export declare namespace obj {
        var x: number;
    }
    export declare namespace obj {
        var y: number;
    }
    export declare function f(): void;
    export declare namespace f {
        var x: number;
    }
    export declare namespace f {
        var y: number;
    }

    Note the missing x and y properties in C and the duplication of the x and y declarations in obj.

  5. weswigham commented on Apr 16, 2026

    @weswigham
    Member

    Do note that the last two items in the OP were intentional to align with the reparsing strategy we were using (you can see the discussion in some of the related PRs), as they allowed trivial syntactic traversal over the reparsed nodes. Aligning those with our old emit, given our now removed cjs reparsing, is mostly a matter of duplicating some binder work and basically having a js declaration pass over the file to collect the same declarations the binder/checker do in order to move them around and transform them - it won't be 100% the same, since we should respect source order way more (strada did not respect it at all), but we should be able to get close. The first issue from the OP is 100% a bug though; not even sure how that snuck in, considering module isn't an actual alias to the current module in-scope, so can't possibly be a name generation result; must be overzealous node reuse somewhere (likely doing a type equality check that doesn't account for js quirks that we still support), which shouldn't be hard to track down.

  6. weswigham commented on May 8, 2026

    @weswigham
    Member

    I think everything other than the "multiple assignments to a module export produce multiple declarations" case from the OP post have been dealt with now. And that I... have weird feelings about. Emitting dupes is "safe" - it's the natural output of a simple syntactic traversal and translation of the cjs assignments, and if the types match exactly, there's no errors. They are, in effect, subsequent declarations of the same type. They can actually have differing type annotations (which trigger a "subsequent variable declarations must have the same type" check error).

    Removing the dupes requires binding those declarations together (and generally operating symbolically like the old js declaration emitter), which means an emit resolver check like the isImplementationOfOverload check. That's certainly not horrible for us to do, but... should we? We don't bind together duplicate declarations in TS declaration emit. Consider if each declaration has different JSDoc comments - should we merge them? Favor one?

    And then, should it at least be an isolatedDeclarations error if there are multiple assignments to the same export member? Since, as I just said, you can't know those assignments are to the same member without binding (and late-binding) declarations together - and late-binding at least is type-based.

    All that makes me think that no - those dupes are fine as-is. They mirror the TS behavior for var redeclarations already.

  7. RyanCavanaugh commented on May 8, 2026

    @RyanCavanaugh
    Member

    Wesley Wigham (@weswigham) is what I'm hearing "wontfix"? I tend to agree if so. Anders Hejlsberg (@ahejlsberg) good w/ you too?

  8. weswigham commented on May 8, 2026

    @weswigham
    Member

    I mean, this was a multi-issue-issue, so retag as you wish, but yeah, that's how I'm feeling about the last bit I identified. Minimally, if there's anything else people feel strongly about, it's probably easier to track and discuss as individual issues.

  9. ahejlsberg commented on Jun 11, 2026

    @ahejlsberg
    MemberAuthor

    Removing the dupes requires binding those declarations together (and generally operating symbolically like the old js declaration emitter), which means an emit resolver check like the isImplementationOfOverload check. That's certainly not horrible for us to do, but... should we?

    For this CJS module

    exports.foo = 42
    exports.foo = "hello"
    exports.foo = true

    we produce the following declaration emit

    export declare var foo: "hello" | 42 | true;
    export declare var foo: "hello" | 42 | true;
    export declare var foo: "hello" | 42 | true;
    

    So, those truly are dupes and the declarations have already been bound together because we infer the declared type of an expando property to be the union of all observed assigned types. I doesn't per se hurt to have those dupes, but they're never going to validate anything because the types are always going to be the same. It's basically just noise and overhead when we process the .d.ts file because we then have to validate they're the same.

  10. weswigham commented on Jun 11, 2026

    @weswigham
    Member

    I doesn't per se hurt to have those dupes, but they're never going to validate anything because the types are always going to be the same

    Not if they have differing type annotations, eg

    exports.foo = 42
    /** @type {string} */
    exports.foo = "hello"
    /** @type {boolean} */
    exports.foo = true

    but our behavior there is weird anyway - it's first in wins (the first declaration we encounter with a declaration decides the type of the export), so you have to sort through all the bound up declarations to actually find the type node for the export. The equivalent TS would emit multiple declarations, persisting the input error (mismatched type nodes) through to the output. In any case, I opened #4284 with the change, since I was able to do the deupe with just simple string matching rather than a full bind (and only the type node lookup gets weird), I don't feel horrible about having other emitters need to copy it. (Though that does mean late-bound exports don't get deduped, but we also don't support those anymore, so...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Domain: Declaration Emitbug

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions