Allow `this` in constructor parameter
See original GitHub issueSearch 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.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:8
- Comments:21 (4 by maintainers)
Top GitHub Comments
@RyanCavanaugh I am speaking as a representative of amCharts.
We use this pattern a lot in our code:
We use this pattern in order to simulate associated types.
We would really like to use
this["_properties"]
, because we often do have classes which inherit from the parent and don’t have a constructor of their own. Right now we have to manually create dozens of useless dummy constructors:Since all normal methods can use
this
, why shouldn’tconstructor
also allow it? It’s a very bizarre exception that seems to serve no purpose.@dl748 That is incorrect. It has been possible for a long time to use
this
andthis["foo"]
in all normal methods. They are not a new feature, and we use them very extensively in our code (which does not use React). They are incredibly useful and not framework specific at all.I know this issue is over a year old at this point, but it’s still open and “awaiting feedback” so I’ll throw in my support for this here rather than opening anew (but I’ll do so if that’s wanted).
I’ve hit a problem with this very issue a few times now, and here’s the latest example, boiled down:
Because I would want my
FooChild
implementation ofChild
to only acceptParent<FooChild>
(or something assignable to it), but this cannot be done without error. If I set the constructor to be more open (say,Parent<Child>
), input of the wrong type could be passed in. This could also be approached with something likeabstract class Child<T extends Parent<any> = Parent<this>>
as mentioned above but that’s also not allowed.Doing this in method arguments and generics in a class is indeed supported (at least in TS ^4), so it seems odd it’s an error in the constructor.