microsoft / microsoft/TypeScript
Compiler should complain about implicit return value of child class constructor, if it does not satisfy child class type
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
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.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
La issue fornisce una riproduzione inline in TypeScript, ma non indica alcun file sorgente o test. Inizia riproducendo il caso del costruttore derivato e tracciando la gestione dei tipi restituiti dai costruttori da parte del compilatore; il lavoro è completato quando il compilatore segnala che un valore restituito implicito del costruttore figlio non può soddisfare il tipo della classe figlia.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 30/100