microsoft / microsoft/TypeScript

Can't use async arrow function in/before super call

Offen
#42,667 2 Kommentare 1 Reaktion 1 zugewiesene Person Auf GitHub ansehen

@rbuckton arbeitet bereits daran.

Seit 05.2.2021.

Bug Domain: classes Rescheduled
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

Bug Report

🔎 Search Terms

super async arrow function this

🕗 Version & Regression Information

This only occurs using a target version of ES2016 or lower.

⏯ Playground Link

https://www.typescriptlang.org/play?target=3&ts=4.1.3#code/MYGwhgzhAECC0G8BQ1rAPYDsIBcBOArsDungBQBmmAXNGQJTQC8AfNAAp7oC2AlhAFMAPADd0vACYtGyVKioMA3CmgBfJOqShIMAELQBADxwDMEmPFlosuQsVINEK1BAIAHAeUgBPTMDqMrE5ychjY6CACAHQg6ADmZABEABICILGJ9Mohqlkq6pqYAgDu0LpKQA

💻 Code

This works:

class A {
  constructor(fn: () => Promise<void>) {
    fn();
  }
}

class B extends A {
  constructor() {
    super(async function () {
      console.log("Hello");
    });
  }
}

new B();

This doesn't:

class A {
  constructor(fn: () => Promise<void>) {
    fn();
  }
}

class B extends A {
  constructor() {
    super(async () => {
      console.log("Hello");
    });
  }
}

new B();

The difference between the examples is the use of an arrow function vs a traditional function.

🙁 Actual behavior

It compiles crashes however at runtime with this error message:
ReferenceError: Must call super constructor in derived class before accessing 'this' or returning from derived constructor

The error lies in the generated __awaiter function trying to access this. The traditional function wraps the __awaiter call (therefore binding its own this) not causing it to fail.

🙂 Expected behavior

Print Hello to the console.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.