TypeScript Version: 3.3.0-dev.20181222
Search Terms: TS2409
Code
class A {
constructor() {
// return an arrow function
return message => {
this._message = message;
return this;
};
}
bar() {
console.log(this._message);
}
}
const a = new A();
a('hello world').bar(); // 'hello world'
Expected behavior:
No error.
Actual behavior:
src/test.ts(4,5): error TS2409: Return type of constructor signature must be assignable to the instance type of the class.
I should be able to override the object returned from a constructor.
Normally constructors don't return a value, but they can choose to do so if they want to override the normal object creation process.
Souce: MDN - new operator
Functions can, classes cannot:

(This may "work" downlevel where classes are compiled to functions, however with real classes it does not)
Functions can, classes cannot:
@weswigham I believe you're mistaken. Class constructors cannot return _other primitives_. They can however return Objects.

Mmm. I suppose we just make the distinction to make a class easier to typecheck.
I have the same issue. Is there a solution around? It seems that Typescript will prevent any class chaining when you want to chain your methods which is bad IMO!!
@weswigham
Hi. I'm wondering about this issue's result.
I know this is (probably) not an error in ESLint. However, it is an error in typescript. So I can't choose which is better. What do you think about this issue now in 2020?
Real life example where returning something other that this is actually very helpful is when we want to make the constructor of a class T async. Due to the nature of how async/await is implemented, the constructor would need to return a Promise<T> instead of T.
class T {
constructor() {
let that = this;
return (async () => {
// ... All async code here
return that;
})();
}
}
However Typescript complains that 'Return type of constructor signature must be assignable to the instance type of the class.' while I believe it should not indicate the above method of returning a Promise<T> as erroneous.
Most helpful comment
@weswigham I believe you're mistaken. Class constructors cannot return _other primitives_. They can however return Objects.