Repository navigation
Allow declare redeclarations of inherited properties in field initializers - #65577
5 (tydavid5) wants to merge 2 commits into
Conversation
…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.
There was a problem hiding this comment.
🟡 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,
declareredeclarations, 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
| // 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)) { |
There was a problem hiding this comment.
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.
| // 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)) { |
There was a problem hiding this comment.
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


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.