microsoft / microsoft/TypeScript
Compiler should complain about implicit return value of child class constructor, if it does not satisfy child class type
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 2 T. 4 Std.
- Gemergte PRs (30 T.)
- 132
Beschreibung
TypeScript Version: 2.1.5
Since TypeScript 2.1 (due to #7574) the return value of a constructor call is the return value of the super constructor. So that
class Child extends Base {}
is compiled to
function Child() {
return _super !== null && _super.apply(this, arguments) || this;
}
Actual behavior:
The actual problem is, that the implementation of Child(see example below) will be compiled without errors, despite of the fact that the instance (child) does not satisfy its type Child, because someMethod does not exist on child.
class Base {
hello = 'base';
constructor() {
return {
hello: 'world'
};
}
}
class Child extends Base { // <-- compiler does not complain
someMethod() {}
}
const child = new Child();
child.someMethod(); // <-- compiler does NOT complain, despite of "someMethod" does not exist on {hello: 'world'}
Expected behavior:
The compiler should complain about the implicit return value of the child class constructor, if this value is not a type of the child class. So that either an explicitly defined return value for the child class constructor should be forced like...
class Child extends Base {
constructor() {
super();
return {
someMethod() {},
/* ... inherited members */
}
}
someMethod() {}
}
or the return value has to be extended (const base = super()) to fulfill the type Child or all members of the child class have to be optional.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Das Issue enthält eine Inline-Reproduktion in TypeScript, nennt aber keine Quelldateien oder Tests. Beginne damit, den Fall des abgeleiteten Konstruktors zu reproduzieren und die Verarbeitung der Rückgabetypen von Konstruktoren durch den Compiler nachzuverfolgen; fertig ist die Aufgabe, wenn der Compiler meldet, dass ein impliziter Rückgabewert des Kindkonstruktors den Typ der Kindklasse nicht erfüllen kann.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 30/100