Skip to content

Allow declare redeclarations of inherited properties in field initializers - #65577

Closed
5 (tydavid5) wants to merge 2 commits into
microsoft:mainfrom
tydavid5:fix/ts2729-declare-field-initializer-65514
Closed

5 (tydavid5) wants to merge 2 commits into
microsoft:mainfrom
tydavid5:fix/ts2729-declare-field-initializer-65514

Conversation

@tydavid5

Copy link
Copy Markdown

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.

Verification: the issue repro gave main.ts(9,14) error TS2729 before the fix and no errors after. The new test passes, and with the fix reverted it fails with exactly the extra TS2729 on the declare case. Eleven edge cases were checked by hand against both binaries, and only the declare cases changed.

…izers

Fixes microsoft#65514

PR microsoft#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.
Copilot AI balanced review requested due to automatic review settings November 19, 2026 11:18
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Nov 19, 2026
@typescript-automation typescript-automation Bot added the For Backlog Bug PRs that fix a backlog bug label Nov 19, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The new exemption can panic on ambient global variables and incorrectly suppress TS2729 for static declarations by checking inherited instance properties.

2 open issues
What changed in this PR

This PR addresses #65514 by allowing field initializers to read inherited properties redeclared with declare, which emits no field that could overwrite the inherited value.

Changes:

  • Adjusts the checker’s ancestor-property exemption under useDefineForClassFields.
  • Adds regression coverage for inherited reads, declare redeclarations, ordinary redeclarations, and own-field forward references.
  • Adds JavaScript and diagnostic baselines for the new test.
File Description
tsc/​testdata/​tests/​cases/​conformance/​classes/​propertyMemberDeclarations/​declarePropertyRedeclaration.ts Adds positive and negative field-initialization cases.
tsc/​testdata/​baselines/​reference/​conformance/​declarePropertyRedeclaration.js Records emitted JavaScript, including removal of the declare field.
tsc/​testdata/​baselines/​reference/​conformance/​declarePropertyRedeclaration.errors.txt Records expected initialization diagnostics.
tsc/​internal/​checker/​checker.go Adjusts the ancestor exemption for ambient declarations.

Review effort: Balanced

Comment thread tsc/internal/checker/checker.go Outdated
// 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)) {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. 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. An ambient global variable no longer reaches the ancestor check, so the panic is gone, and the case now reports TS2729 exactly as main does. Added a regression case for the global variable access, plus a control where the same access happens from an instance field initializer.

One note: with useDefineForClassFields off, main also panics on this access, because the ancestor check itself has no parent guard. That is pre-existing and out of scope for this PR, so I left that path untouched.

Comment thread tsc/internal/checker/checker.go Outdated
// 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)) {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. The exemption now applies only when the redeclaration is an ambient instance property declaration, so a static declare redeclaration goes through the original check again and TS2729 fires as it should. Added a regression case for the static declare redeclaration, plus a control where a static field reads an inherited static property.

Copilot review on PR microsoft#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 microsoft#65514
@github-project-automation github-project-automation Bot moved this from Not started to Done in PR Backlog Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Backlog Bug PRs that fix a backlog bug

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

TS2729 when a field initializer reads an inherited property redeclared with declare

3 participants