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.

Allow `this` in constructor parameter

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:8
  • Comments:21 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
Pauancommented, Aug 1, 2020

@RyanCavanaugh I am speaking as a representative of amCharts.

We use this pattern a lot in our code:

export interface IFoo {
    ...
}

export class Foo {
    declare public _properties: IFoo;
 
    constructor(properties: Foo["_properties"]) {
        ...
    }
}

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:

export interface IBar extends IFoo {
    ...
}

export class Bar extends Foo {
    declare _properties: IBar;

    constructor(properties: Bar["_properties"]) {
        super(properties);
    }
}

Since all normal methods can use this, why shouldn’t constructor 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 and this["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.

5reactions
CynicalBusinesscommented, Oct 1, 2021

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:

class Parent<T extends Child> {
    // ...
}

abstract class Child {
    public constructor (parent: Parent<this>) { // ts(2526)
        // ...
    }
}

Because I would want my FooChild implementation of Child to only accept Parent<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 like abstract 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Passing Information to a Method or a Constructor
When you declare a parameter to a method or a constructor, you provide a name for that parameter. This name is used within...
Read more >
How do I pass a parameter to the constructor using Simple ...
I would prefer to use the parameter in the constructor to be sure that the parameter is really set before doing something with...
Read more >
Object orientation - The Apache Groovy programming language
Groovy supports two invocation styles: positional parameters are used in a similar to how you would use Java constructors. named parameters allow you...
Read more >
constructor - JavaScript - MDN Web Docs - Mozilla
The constructor method is a special method of a class for creating ... parent constructor, passing along any arguments that were provided:.
Read more >
Constructors - F# | Microsoft Learn
One is the primary constructor, whose parameters appear in ... about let bindings in class constructors, see let Bindings in Classes.
Read more >

github_iconTop Related Medium Post

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