Skip to content

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

Open

Description

🔎 Search Terms

TS2729, used before its initialization, declare, useDefineForClassFields

🕗 Version & Regression Information

⏯ Playground Link

https://www.typescriptlang.org/play/?useDefineForClassFields=true&target=99&ts=6.0.3#code/MYGwhgzhAEBCkFNoG8BQ0PQEYC5oDsBXAWywQCdoBeaARgG5UBfVVUSGABXAEt9oEADwAuCfABMY8CEjSZo46tGEALHhAB0WetAD0u6AHkA0s1bso0AHJhy5APYB3BIqGiJUxCnSZxCduRIuHTQAD7QAEyM8oo0qupaOvoCdvaUACoAyhEA7BEAnHicDgAOFMIAntAA5FjV0OrQhDKKZABmaUg8wjB83TxgIDwAXmDCPPb4GmZskxD2IAgaIPYA5gAU+AiO1rYOzuLrAJQa4kdJBrSoQA

💻 Code

(The Playground does not derive useDefineForClassFields from the target, so the link above enables it explicitly.)

class Base {
    b: number = 1;
}

class Plain extends Base {
    d = this.b; // OK
}

class Narrowed extends Base {
    declare b: 1 | 2;
    d = this.b; // error TS2729: Property 'b' is used before its initialization.
}

console.log(new Narrowed().d); // 1

🙁 Actual behavior

this.b in Narrowed reports TS2729.

🙂 Expected behavior

No error, as in Plain. Per the spec, the base class's fields are initialized inside super(), and the derived class's fields right after super() returns (SuperCall evaluation). declare emits nothing, so b is already initialized (1) when d is initialized. #42319 also lists this as expected: "c is allowed because of the declare modifier".

Additional information about the issue

  • Consider class field properties to redeclare parent definitions #43194 removed the ancestor-class exemption under useDefineForClassFields, for the case where a redeclaration without an initializer (b;) overwrites the property with undefined (DefineField). It also catches declare redeclarations, which emit nothing. The test it added covers only the case without declare.
  • TS2612 suggests adding declare to such a redeclaration, and doing so produces this error.
  • The same error occurs with declare context: React.ContextType<…>, as recommended by @types/react, when a field initializer reads this.context.
  • Found while testing my own TypeScript compiler (https://github.com/mizchi/ts.mbt).

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: classesBehavior of various `class` constructs, e.g. mixins or base classes

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions