From 7791e4a48d0bdd982f0184f7fc14545a4e980b9d Mon Sep 17 00:00:00 2001 From: 5 <93879550+tydavid5@users.noreply.github.com> Date: Thu, 19 Nov 2026 12:17:46 +0100 Subject: [PATCH 1/2] Allow declare redeclarations of inherited properties in field initializers Fixes #65514 PR #43194 removed the ancestor class exemption for the TS2729 check under useDefineForClassFields, because a redeclaration without an initializer overwrites the base class field with undefined. That change also caught declare redeclarations, which emit nothing, so the base field keeps the value it was given in super() and reading it in a derived field initializer is fine. The check now keeps the ancestor exemption when the redeclaration is a declare property declaration, since there is nothing to overwrite. A redeclaration without declare and without an initializer still errors, as does a forward reference to an own class field and a parameter property redeclaration, and the new regression test covers those cases alongside the fixed one. --- tsc/internal/checker/checker.go | 6 +- .../declarePropertyRedeclaration.errors.txt | 39 +++++++++++++ .../declarePropertyRedeclaration.js | 55 +++++++++++++++++++ .../declarePropertyRedeclaration.ts | 32 +++++++++++ 4 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 tsc/testdata/baselines/reference/conformance/declarePropertyRedeclaration.errors.txt create mode 100644 tsc/testdata/baselines/reference/conformance/declarePropertyRedeclaration.js create mode 100644 tsc/testdata/tests/cases/conformance/classes/propertyMemberDeclarations/declarePropertyRedeclaration.ts diff --git a/tsc/internal/checker/checker.go b/tsc/internal/checker/checker.go index 694fc155b8e16..977e522a2b32c 100644 --- a/tsc/internal/checker/checker.go +++ b/tsc/internal/checker/checker.go @@ -11994,7 +11994,11 @@ func (c *Checker) checkPropertyNotUsedBeforeDeclaration(prop *ast.Symbol, node * !(ast.IsAccessExpression(node) && ast.IsAccessExpression(node.Expression())) && !c.isBlockScopedNameDeclaredBeforeUse(valueDeclaration, right) && !(ast.IsMethodDeclaration(valueDeclaration) && c.getCombinedModifierFlagsCached(valueDeclaration)&ast.ModifierFlagsStatic != 0) && - (c.compilerOptions.GetUseDefineForClassFields() || !c.isPropertyDeclaredInAncestorClass(prop)) { + // A `declare` redeclaration emits nothing, so unlike a redeclaration + // without an initializer it cannot overwrite the base class field with + // undefined (see #43194). The base field is initialized in super(), so + // the ancestor exemption still applies for a `declare` redeclaration. + ((c.compilerOptions.GetUseDefineForClassFields() && c.getCombinedModifierFlagsCached(valueDeclaration)&ast.ModifierFlagsAmbient == 0) || !c.isPropertyDeclaredInAncestorClass(prop)) { diagnostic = c.error(right, diagnostics.Property_0_is_used_before_its_initialization, declarationName) } else if ast.IsClassDeclaration(valueDeclaration) && !ast.IsTypeReferenceNode(node.Parent) && valueDeclaration.Flags&ast.NodeFlagsAmbient == 0 && !c.isBlockScopedNameDeclaredBeforeUse(valueDeclaration, right) { diagnostic = c.error(right, diagnostics.Class_0_used_before_its_declaration, declarationName) diff --git a/tsc/testdata/baselines/reference/conformance/declarePropertyRedeclaration.errors.txt b/tsc/testdata/baselines/reference/conformance/declarePropertyRedeclaration.errors.txt new file mode 100644 index 0000000000000..1438c1d7b0fdc --- /dev/null +++ b/tsc/testdata/baselines/reference/conformance/declarePropertyRedeclaration.errors.txt @@ -0,0 +1,39 @@ +declarePropertyRedeclaration.ts(16,14): error TS2729: Property 'b' is used before its initialization. +declarePropertyRedeclaration.ts(25,14): error TS2729: Property 'b' is used before its initialization. + + +==== declarePropertyRedeclaration.ts (2 errors) ==== + class Base { + b: number = 1; + } + + class Plain extends Base { + d = this.b; + } + + class Narrowed extends Base { + declare b: 1 | 2; + d = this.b; + } + + class Redeclared extends Base { + b: number; + d = this.b; + ~ +!!! error TS2729: Property 'b' is used before its initialization. +!!! related TS2728 declarePropertyRedeclaration.ts:15:5: 'b' is declared here. + + constructor() { + super(); + this.b = 2; + } + } + + class Own { + d = this.b; + ~ +!!! error TS2729: Property 'b' is used before its initialization. +!!! related TS2728 declarePropertyRedeclaration.ts:26:5: 'b' is declared here. + b = 1; + } + \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/conformance/declarePropertyRedeclaration.js b/tsc/testdata/baselines/reference/conformance/declarePropertyRedeclaration.js new file mode 100644 index 0000000000000..4d97aa022edf6 --- /dev/null +++ b/tsc/testdata/baselines/reference/conformance/declarePropertyRedeclaration.js @@ -0,0 +1,55 @@ +//// [tests/cases/conformance/classes/propertyMemberDeclarations/declarePropertyRedeclaration.ts] //// + +//// [declarePropertyRedeclaration.ts] +class Base { + b: number = 1; +} + +class Plain extends Base { + d = this.b; +} + +class Narrowed extends Base { + declare b: 1 | 2; + d = this.b; +} + +class Redeclared extends Base { + b: number; + d = this.b; + + constructor() { + super(); + this.b = 2; + } +} + +class Own { + d = this.b; + b = 1; +} + + +//// [declarePropertyRedeclaration.js] +"use strict"; +class Base { + b = 1; +} +class Plain extends Base { + d = this.b; +} +class Narrowed extends Base { + d = this.b; +} +class Redeclared extends Base { + b; + d = this.b; + constructor() { + super(); + this.b = 2; + } +} +class Own { + d = this.b; + b = 1; +} diff --git a/tsc/testdata/tests/cases/conformance/classes/propertyMemberDeclarations/declarePropertyRedeclaration.ts b/tsc/testdata/tests/cases/conformance/classes/propertyMemberDeclarations/declarePropertyRedeclaration.ts new file mode 100644 index 0000000000000..bf9f3c1c76ca8 --- /dev/null +++ b/tsc/testdata/tests/cases/conformance/classes/propertyMemberDeclarations/declarePropertyRedeclaration.ts @@ -0,0 +1,32 @@ +// @noTypesAndSymbols: true +// @strictNullChecks: true +// @target: esnext +// @useDefineForClassFields: true + +class Base { + b: number = 1; +} + +class Plain extends Base { + d = this.b; +} + +class Narrowed extends Base { + declare b: 1 | 2; + d = this.b; +} + +class Redeclared extends Base { + b: number; + d = this.b; + + constructor() { + super(); + this.b = 2; + } +} + +class Own { + d = this.b; + b = 1; +} From ec9efde51eb5a7203793a285b0bfed80aa074ecf Mon Sep 17 00:00:00 2001 From: 5 <93879550+tydavid5@users.noreply.github.com> Date: Thu, 19 Nov 2026 12:45:53 +0100 Subject: [PATCH 2/2] Restrict the declare exemption to ambient instance properties Copilot review on PR #65577 caught two gaps in the first version of this fix. The exemption applied to any declaration carrying the Ambient modifier, so an ambient global variable reached isPropertyDeclaredInAncestorClass, whose unconditional parent symbol access panics because a global has no parent class. It also covered static declarations, but the ancestor check only searches the base class's instance properties, so a static declare redeclaration wrongly suppressed TS2729 even though no inherited static property exists. The exemption now applies only when the redeclaration is an ambient instance property declaration, a property declaration with the Ambient modifier and without the Static modifier. Ambient globals and static declarations go through the original check again, matching main exactly, and the regression test covers both Copilot cases plus the controls that must stay clean, an ambient global read from an instance field initializer and a static field reading an inherited static property. Fixes #65514 --- tsc/internal/checker/checker.go | 18 ++++-- .../declarePropertyRedeclaration.errors.txt | 44 +++++++++++++- .../declarePropertyRedeclaration.js | 58 +++++++++++++++++++ .../declarePropertyRedeclaration.ts | 34 +++++++++++ 4 files changed, 148 insertions(+), 6 deletions(-) diff --git a/tsc/internal/checker/checker.go b/tsc/internal/checker/checker.go index 977e522a2b32c..b8dba03f0033e 100644 --- a/tsc/internal/checker/checker.go +++ b/tsc/internal/checker/checker.go @@ -11989,16 +11989,24 @@ func (c *Checker) checkPropertyNotUsedBeforeDeclaration(prop *ast.Symbol, node * } var diagnostic *ast.Diagnostic declarationName := right.Text() + // A `declare` instance property redeclaration emits nothing, so unlike a + // redeclaration without an initializer it cannot overwrite the base class + // field with undefined (see #43194). The base field is initialized in + // super(), so the ancestor exemption still applies for such a + // redeclaration. The exemption is limited to instance property + // declarations: an ambient global variable has no parent class, so + // checking it against an ancestor class would panic, and a static + // declaration must not be matched against the base class's instance + // properties. + isAmbientInstanceProperty := ast.IsPropertyDeclaration(valueDeclaration) && + c.getCombinedModifierFlagsCached(valueDeclaration)&ast.ModifierFlagsAmbient != 0 && + c.getCombinedModifierFlagsCached(valueDeclaration)&ast.ModifierFlagsStatic == 0 if c.isInPropertyInitializerOrClassStaticBlock(node, false /*ignoreArrowFunctions*/) && !c.isOptionalPropertyDeclaration(valueDeclaration) && !(ast.IsAccessExpression(node) && ast.IsAccessExpression(node.Expression())) && !c.isBlockScopedNameDeclaredBeforeUse(valueDeclaration, right) && !(ast.IsMethodDeclaration(valueDeclaration) && c.getCombinedModifierFlagsCached(valueDeclaration)&ast.ModifierFlagsStatic != 0) && - // A `declare` redeclaration emits nothing, so unlike a redeclaration - // without an initializer it cannot overwrite the base class field with - // undefined (see #43194). The base field is initialized in super(), so - // the ancestor exemption still applies for a `declare` redeclaration. - ((c.compilerOptions.GetUseDefineForClassFields() && c.getCombinedModifierFlagsCached(valueDeclaration)&ast.ModifierFlagsAmbient == 0) || !c.isPropertyDeclaredInAncestorClass(prop)) { + (c.compilerOptions.GetUseDefineForClassFields() && !isAmbientInstanceProperty || !c.isPropertyDeclaredInAncestorClass(prop)) { diagnostic = c.error(right, diagnostics.Property_0_is_used_before_its_initialization, declarationName) } else if ast.IsClassDeclaration(valueDeclaration) && !ast.IsTypeReferenceNode(node.Parent) && valueDeclaration.Flags&ast.NodeFlagsAmbient == 0 && !c.isBlockScopedNameDeclaredBeforeUse(valueDeclaration, right) { diagnostic = c.error(right, diagnostics.Class_0_used_before_its_declaration, declarationName) diff --git a/tsc/testdata/baselines/reference/conformance/declarePropertyRedeclaration.errors.txt b/tsc/testdata/baselines/reference/conformance/declarePropertyRedeclaration.errors.txt index 1438c1d7b0fdc..8914884a38e70 100644 --- a/tsc/testdata/baselines/reference/conformance/declarePropertyRedeclaration.errors.txt +++ b/tsc/testdata/baselines/reference/conformance/declarePropertyRedeclaration.errors.txt @@ -1,8 +1,10 @@ declarePropertyRedeclaration.ts(16,14): error TS2729: Property 'b' is used before its initialization. declarePropertyRedeclaration.ts(25,14): error TS2729: Property 'b' is used before its initialization. +declarePropertyRedeclaration.ts(33,27): error TS2729: Property 'v' is used before its initialization. +declarePropertyRedeclaration.ts(50,21): error TS2729: Property 'b' is used before its initialization. -==== declarePropertyRedeclaration.ts (2 errors) ==== +==== declarePropertyRedeclaration.ts (4 errors) ==== class Base { b: number = 1; } @@ -36,4 +38,44 @@ declarePropertyRedeclaration.ts(25,14): error TS2729: Property 'b' is used befor !!! related TS2728 declarePropertyRedeclaration.ts:26:5: 'b' is declared here. b = 1; } + + // An ambient global has no parent class, so reading it in a static field + // initializer must not crash the compiler. The access is still reported, + // because the static initializer runs at class definition time. + class AmbientGlobalStatic { + static x = globalThis.v; + ~ +!!! error TS2729: Property 'v' is used before its initialization. +!!! related TS2728 declarePropertyRedeclaration.ts:36:13: 'v' is declared here. + } + + declare var v: number; + + // The same access from an instance field initializer is fine, because the + // initializer runs at construction time, after the module has evaluated. + class AmbientGlobalInstance { + x = globalThis.w; + } + + declare var w: number; + + // A static declaration must not be exempted: the ancestor check only looks + // at the base class's instance properties, and there is no inherited static + // b here, so Derived.d is undefined at runtime. + class StaticDeclared extends Base { + static d = this.b; + ~ +!!! error TS2729: Property 'b' is used before its initialization. +!!! related TS2728 declarePropertyRedeclaration.ts:51:20: 'b' is declared here. + declare static b: number; + } + + // A static field reading an inherited static property is fine. + class StaticBase { + static b = 1; + } + + class StaticDerived extends StaticBase { + static d = this.b; + } \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/conformance/declarePropertyRedeclaration.js b/tsc/testdata/baselines/reference/conformance/declarePropertyRedeclaration.js index 4d97aa022edf6..762b126c525f4 100644 --- a/tsc/testdata/baselines/reference/conformance/declarePropertyRedeclaration.js +++ b/tsc/testdata/baselines/reference/conformance/declarePropertyRedeclaration.js @@ -28,6 +28,40 @@ class Own { d = this.b; b = 1; } + +// An ambient global has no parent class, so reading it in a static field +// initializer must not crash the compiler. The access is still reported, +// because the static initializer runs at class definition time. +class AmbientGlobalStatic { + static x = globalThis.v; +} + +declare var v: number; + +// The same access from an instance field initializer is fine, because the +// initializer runs at construction time, after the module has evaluated. +class AmbientGlobalInstance { + x = globalThis.w; +} + +declare var w: number; + +// A static declaration must not be exempted: the ancestor check only looks +// at the base class's instance properties, and there is no inherited static +// b here, so Derived.d is undefined at runtime. +class StaticDeclared extends Base { + static d = this.b; + declare static b: number; +} + +// A static field reading an inherited static property is fine. +class StaticBase { + static b = 1; +} + +class StaticDerived extends StaticBase { + static d = this.b; +} //// [declarePropertyRedeclaration.js] @@ -53,3 +87,27 @@ class Own { d = this.b; b = 1; } +// An ambient global has no parent class, so reading it in a static field +// initializer must not crash the compiler. The access is still reported, +// because the static initializer runs at class definition time. +class AmbientGlobalStatic { + static x = globalThis.v; +} +// The same access from an instance field initializer is fine, because the +// initializer runs at construction time, after the module has evaluated. +class AmbientGlobalInstance { + x = globalThis.w; +} +// A static declaration must not be exempted: the ancestor check only looks +// at the base class's instance properties, and there is no inherited static +// b here, so Derived.d is undefined at runtime. +class StaticDeclared extends Base { + static d = this.b; +} +// A static field reading an inherited static property is fine. +class StaticBase { + static b = 1; +} +class StaticDerived extends StaticBase { + static d = this.b; +} diff --git a/tsc/testdata/tests/cases/conformance/classes/propertyMemberDeclarations/declarePropertyRedeclaration.ts b/tsc/testdata/tests/cases/conformance/classes/propertyMemberDeclarations/declarePropertyRedeclaration.ts index bf9f3c1c76ca8..eff307c3ba26d 100644 --- a/tsc/testdata/tests/cases/conformance/classes/propertyMemberDeclarations/declarePropertyRedeclaration.ts +++ b/tsc/testdata/tests/cases/conformance/classes/propertyMemberDeclarations/declarePropertyRedeclaration.ts @@ -30,3 +30,37 @@ class Own { d = this.b; b = 1; } + +// An ambient global has no parent class, so reading it in a static field +// initializer must not crash the compiler. The access is still reported, +// because the static initializer runs at class definition time. +class AmbientGlobalStatic { + static x = globalThis.v; +} + +declare var v: number; + +// The same access from an instance field initializer is fine, because the +// initializer runs at construction time, after the module has evaluated. +class AmbientGlobalInstance { + x = globalThis.w; +} + +declare var w: number; + +// A static declaration must not be exempted: the ancestor check only looks +// at the base class's instance properties, and there is no inherited static +// b here, so Derived.d is undefined at runtime. +class StaticDeclared extends Base { + static d = this.b; + declare static b: number; +} + +// A static field reading an inherited static property is fine. +class StaticBase { + static b = 1; +} + +class StaticDerived extends StaticBase { + static d = this.b; +}