Skip to content

7.0 regression: expando property on a function declaration is dropped when a merged namespace exports a type with the same name (TS2339) #65600

Open

Description

🔎 Search Terms

expando, expando namespace, namespace merge, function namespace merge, property assignment, type-only namespace, TS2339, "does not exist on type", compound component, static property

🕗 Version & Regression Information

⏯ Playground Link

https://www.typescriptlang.org/play/v2/?ts=next#code/v2/N4IghgxgLglgbgUwGIwDYJALhAegO4D2ATgNYDOADpAjlGRAQHYBmMA5gHQBWZTIANCFboyWUPmLkqEGnQYt23XoywhgAAgA6IBgFsKaBEQDyFWEzLbM6jdrJQiMaFfUOArgn5aQUMETYIUC7aCGQATAAMYWHa6gC+8QK4hKSU1DhkRBA4MIwAJggAHhx0qsxujNAwTOrMABQAlDZxmiwcYOoAvOoRANytjGC6odIItTbqRRTEUK4AnhRjHd198a0gcYJkCOhVfJig02Qw5owAwgSobroqmABsYYJHJ9WMADK5CABy1wBGRlgAIxbHYIPaMADKviIUAuVxuWAeIN2pyhfigH0Y3z+AMwgM2IEQRGO+zCcSAA

💻 Code

function f() {}
f.a = 0;
namespace f { export type a = 0; }

Real-world shape (this is how Mantine declares its compound components, e.g. Popover.Target with a Popover.Target.Props type):

export function Popover() { return null; }
Popover.Target = (x: number) => x;
Popover.displayName = "Popover";

export namespace Popover {
  export namespace Target {
    export type Props = number;
  }
}

Popover.Target(1);
Popover.displayName;

🙁 Actual behavior

7.0.2, 7.1.0-beta and 7.1.0-dev.20261120.1:

min.ts(2,3): error TS2339: Property 'a' does not exist on type '() => void'.
repro.ts(2,9): error TS2339: Property 'Target' does not exist on type '{ (): null; displayName: string; }'.
repro.ts(11,9): error TS2339: Property 'Target' does not exist on type '{ (): null; displayName: string; }'.

The value property a (or Target) is removed from the function's type because the merged namespace has a type-only export with the same name. displayName, which has no namespace counterpart, is kept.

🙂 Expected behavior

No error, as in 6.0.3. The namespace member is type-only (a type alias, an interface, or a namespace that contains only types), so it does not declare a value named a. The expando assignment is the only value declaration for that name and should still create the property. In 6.0.3, f.a is typed as number (const s: string = f.a reports TS2322 "Type 'number' is not assignable to type 'string'").

Additional information about the issue

From reading the binder, an expando is now declared only when the name has no existing non-expando declaration:

// tsc/internal/binder/binder.go
// We declare expandos only when there are no non-expando declarations for that name.
if existing := exports.Get(b.getDeclarationName(node)); existing == nil || existing.Flags()&ast.SymbolFlagsAssignment != 0 {

This check does not appear to consider whether the existing declaration has a value meaning, so a type alias, an interface or an uninstantiated namespace also suppresses the property. #2715 was prompted by a value conflict (microsoft/typescript-go#2653: a declare class static member plus an expando assignment).

Results by variant, each file ending with const n: number = f.a; (--strict --noEmit --target es2022):

Variant 6.0.3 7.0.2
export type a in the namespace (minimal repro) no error TS2339
export interface a {} in the namespace no error TS2339
export namespace a {} (empty) no error TS2339
export namespace a containing only types no error TS2339
namespace declared before the function no error TS2339
non-exported type a in the namespace no error no error
namespace without a member named a no error no error
no namespace at all no error no error
const f = () => {} instead of function f() {} no error no error
property declared in the type (declare const f: { (): void; a: number }) no error no error
namespace member a also contains a value (export const kind = 0) TS2300 Duplicate identifier TS2322 (different errors, both reject the code)

Metadata

Metadata

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