Skip to content

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

Closed
@ajafff

Description

@ajafff

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 @sandersn

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions