microsoft / microsoft/TypeScript

TS2409 is an invalid error.

Open
#29,138 7 comments 11 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the TypeScript code sample and investigate how constructor return types are checked against the class instance type. Compare the reported TS2409 behavior with JavaScript's constructor-return semantics; done means the intended valid case is accepted without weakening unrelated constructor checking.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.