Search code examples
typescripttypescript-eslint

Eslint rule to detect initialized class properties


Using useDefineForClassFields:true the following code is not valid anymore when targeting es2022 : Cannot read properties of undefined (reading 'array')

class Bar {
    array = new Array();
}

class Foo {
    foo = this.bar.array; // Property 'bar' is used before its initialization

    constructor(private bar: Bar) {}
}

console.log(new Foo(new Bar()))

Is there an ESLint rule to trigger an error on this ?

PS: The target EsNext is triggering an error, but this is not what i'm looking for.

Playground


Solution

  • As CaTS suggest, this should be a tsc error.

    There is an open bug about this.