Skip to content

Execution branches not evaluated for super/this calls #38512

Closed
@ricmoo

Description

@ricmoo

Search Terms:
super, this and variations of "'super' must be called before accessing 'this' in the constructor of a derived class."

Code

export class Foo {
  constructor(value: number) {
  }
}

export class BarCorrectlyFails extends Foo {
    constructor(something: boolean) {
      if (!something) {
        const value = this.bar();  // <--- this used before super; correctly detected
        super(value);
      } else {
        super(1337);
      }
    }
    bar(): number { return 4; }
}

export class BarIncorrectlyWorks extends Foo {
    constructor(something: boolean) {
      if (something) {
        super(1337);
      } else {
        const value = this.bar();  // <--- this used before super; not detected
        super(value);
      }
    }
    bar(): number { return 4; }
}

Expected behavior:

Both of the above Bar* classes should fail to compile. The branches seem to be ignored when determining if this and super are called in-order, so the above super call in another branch seems to allow compilation to continue.

Actual behavior:

The BarIncorrectlyWorks produces ESM code which fails at runtime, when the super occurs before the this.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions