Skip to content

Declaration emit aliases an expando member to an out-of-scope or different identifier when the assigned value is an identifier #65615

Open

Description

TypeScript Version

The tested TypeScript version is 7.0.2 (also main at 002fa0a; the code is unchanged at 01f1a75). 6.0.3 is correct.

User Goal

The user maintains a fuzzer for TypeScript declaration emit and asked me to find real bugs and report them. This one was found by probing the code around past expando fixes.

Setup and Action

// lib.ts
export function f() {}
for (const k of ["a", "b"]) {
    f.last = k;
}
// use.ts
import { f } from "./out/lib.js";
const s: string = f.last;

Run tsc --declaration --emitDeclarationOnly --strict --outDir out lib.ts, then check use.ts against out/lib.d.ts.

Expected and Actual Behavior

What actually happened: when the right-hand side is an identifier, 7.x emits export { <identifier> as <member> } inside the namespace, without checking that the identifier means the same thing at the top level:

export declare function f(): void;
export declare namespace f {
    export { k as last };
}

k does not exist at the top level, so use.ts fails with TS2304: Cannot find name 'k'. The same branch gives a wrong .d.ts in other cases too:

Source 7.x .d.ts Consumer result
Block-scoped k shadows a top-level const k = 1 export { k as last } f.last is number (TS2322); it should be string
if (typeof v === "string") f.s = v; with v: string | number export { v as s } f.s is string | number; narrowing is lost
const VERSION = "1.2.3"; f.version = VERSION; export { VERSION as version } f.version is "1.2.3"; assigning "2.0.0" fails (TS2322)
f.n = initial; f.n = 5; export { initial as n } plus export var n: number The .d.ts itself fails with TS2484

What I expected: what 6.0.3 emits, which uses the property's type:

export declare namespace f {
    var last: string;
}

In transformExpandoAssignment (internal/transformers/declarations/transform.go), the ast.IsIdentifier(node.Right) branch calls transformBinaryExpressionToExportDeclaration without checking that the identifier resolves to the same symbol from the namespace's enclosing scope, or that the symbol's declared type matches the member's type.

Agent Info

Claude Code + Claude Opus 5.5 + Skill Version 1.0

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions