microsoft / microsoft/TypeScript

Allow `this` in constructor parameter

Open
#38,038 44 comments 32 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

Search Terms

type, this, constructor, TS2526

Suggestion

Currently, this code fails with A 'this' type is available only in a non-static member of a class or interface.ts(2526):

class MyClass {
    private props: { baz: string };
    constructor(props: this["props"]) {
       this.props = props;
    }
}

It would be awesome if this could be supported in constructor arguments.

Use Cases

This is very useful for react components, as demonstrated in the following example.
More general, sometimes a class extends a generic base class which requires generic data for initialization. If the super class also wants to do some initialization, it needs to pass this generic argument down to its base class. As constructor arguments must be typed, the type of this generic argument must be explicitly stated in the super class.
In those cases, it is very practical to use this to refer to a field of that base class that has the right type.

Examples

class MyComponent extends React.Component<{
    title: string;
}> {
    constructor(props: this["props"]) {
       super(props);
       // ...
    }
}

Current Workarounds

class MyComponent extends React.Component<{
    title: string;
}> {
    constructor(props: MyComponent["props"]) {
       super(props);
       // ...
    }
}

This only works if the base class declares a static props member.
Also, it has the downside that you must write out the name of the current class. This obfuscates the fact that it refers itself.

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

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 by reproducing TS2526 with the constructor examples in the issue and trace how TypeScript validates this types in constructor parameters. The issue names no source files or tests; done means both examples accept this["props"] without changing emitted JavaScript, with coverage for the shown cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.