Repository navigation
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
- This changed between versions 6.0.3 and 7.0.2 (still present in 7.1.0-beta and 7.1.0-dev.20261120.1)
- This changed in commit or PR Defer binding of expando assignments and give them lower priority typescript-go#2715 ("Defer binding of expando assignments and give them lower priority").
@typescript/native-preview@7.0.0-dev.20260209.1has no error,7.0.0-dev.20260210.1reports it. What do we call this command-line option? #2715 is the only expando-related commit between those two builds.
⏯ Playground Link
💻 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) |
Activity