Skip to content

Missing use-before-def error for redeclared property present in base class #42319

Closed

Description

Bug Report

🔎 Search Terms

  • used before extends
  • property initialization base
  • useDefineForClassFields initialization

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about useDefineForClassFields

⏯ Playground Link

Playground link with relevant code

💻 Code

// @useDeclareForClassFields: true
class Base {
    a = 1;
    b = 1;
    c = 1;
    d = 1;
    e = 1;
}

class Derived extends Base {
    a = 2;
    b;
    declare c: number;
    d: number = this.a /*2*/ + this.b /*undefined*/ + this.c /*1*/ + this.d /*1*/ + this.e /*1*/; // expected error on 'b', all other are valid
    e;

    constructor() {
        super();
        this.b = 2;
        this.e = 2;
    }
}

🙁 Actual behavior

No error in the code above. Use-before-def allows access to properties of the base class, even if the current class redeclares that property.
This is correct until you enable useDefineForClassFields. Then the redeclaration of b overrides the base class property before the access. I added comments containing the runtime values.

🙂 Expected behavior

  • a is allowed because it's initialized
  • b is a use-before-def error
  • c is allowed because of the declare modifier
  • d is allowed to access the base class property in the initializer becaues at that point its value is not overridden yet
  • e is allowed because its value is overridden after accessing it.

/cc Nathan Shively-Sanders (@sandersn)

Activity

  1. ajafff commented on Jan 14, 2021

    @ajafff
    ContributorAuthor

    There's a very similar issue when a base class property is redeclared as parameter property. Note that this one requires target: ESNext AND useDefineForClassFields:

    class Base {
        a = 1;
    }
    
    class Derived extends Base {
        b = this.a /*undefined*/;
    
        constructor(public a: number) {
            super();
        }
    }

    For this case the error logic needs to be chaned to: if emitting native class fields, it's not allowed to use parameter properties in other property's initializers, regardless of the presence of a base class property with the same name.

  2. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScript

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions