-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Description
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.