You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Sep 1, 2026. It is now read-only.
Repository navigation
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
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.
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.
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.
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.
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=42exports.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.
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...)
Activity
RyanCavanaugh commentedon Apr 14, 2026
Confirmed all three still exist in nightly
ahejlsberg commentedon Apr 14, 2026
Note that we support multiple assignments to
module.exportsandexports.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.ahejlsberg commentedon Apr 14, 2026
There are other JS declaration emit issues. For this JS file:
We generate this declaration file:
Note the missing
xandyproperties inCand the duplication of thexandydeclarations inobj.weswigham commentedon Apr 16, 2026
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
moduleisn'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.weswigham commentedon May 8, 2026
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
isImplementationOfOverloadcheck. 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
isolatedDeclarationserror 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
varredeclarations already.RyanCavanaugh commentedon May 8, 2026
Wesley Wigham (@weswigham) is what I'm hearing "wontfix"? I tend to agree if so. Anders Hejlsberg (@ahejlsberg) good w/ you too?
weswigham commentedon May 8, 2026
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.
ahejlsberg commentedon Jun 11, 2026
For this CJS module
we produce the following declaration emit
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.
weswigham commentedon Jun 11, 2026
Not if they have differing type annotations, eg
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...)