question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TS2409 is an invalid error.

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:10
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
willemmuldercommented, Oct 8, 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.

3reactions
michaelhartmayercommented, Dec 25, 2018

Functions can, classes cannot:

@weswigham I believe you’re mistaken. Class constructors cannot return other primitives. They can however return Objects.

image

Read more comments on GitHub >

github_iconTop Results From Across the Web

Return type of constructor signature must be assignable to the ...
compilation is good but with this type error :TS2409: Return type of constructor signature must be assignable to the instance type of the ......
Read more >
Error & Warning Details - typegoose
This Error may get thrown when a invalid Type is used with the option enum . Currently Valid Types for enum are String...
Read more >
CreateWorldGenerationJobResponse | RoboMaker Client - AWS ...
The specified resource could not be found. RequestThrottled. The request was throttled. InvalidInput. An input parameter in the request is not valid.
Read more >
ERROR_CODE_TYPE | Video SDK v1.3.1_3.1 API Reference for ...
1115: Audio device module: AudioRecord returns error. ... Defined in agora.ts:2409 ... The token is invalid due to one of the following reasons:....
Read more >
googleapis documentation - Google Cloud
Defined in src/apis/privateca/v1beta1.ts:2409. Parameters : ... Requests specifying an invalid value will be rejected. Requests for policies with any ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found