Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ajafff opened this issue Jan 13, 2021 · 1 comment · Fixed by #43194
Closed

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

ajafff opened this issue Jan 13, 2021 · 1 comment · Fixed by #43194
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@ajafff
Copy link
Contributor

ajafff commented Jan 13, 2021

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

@ajafff
Copy link
Contributor Author

ajafff commented Jan 14, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants