microsoft / microsoft/TypeScript
Can't use async arrow function in/before super call
@rbuckton y travaille déjà.
Depuis le 5/2/2021.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.3k
- Merge moyen
- 2 j 4 h
- PR mergées (30 j)
- 132
Description
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
💻 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.
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Évaluation
Cette issue n'a pas encore été évaluée.