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.

T.constructor should be of type T

See original GitHub issue

Given

class Example {
}

The current type of Example.constructor is Function, but I feel that it should be typeof Example instead. The use case for this is as follows:

I’d like to reference the current value of an overridden static property on the current class.

In TypeScript v1.5-beta, doing this requires:

class Example {
    static someProperty = "Hello, world!";

    constructor() {
        // Output overloaded value of someProperty, if it is overloaded.
        console.log(
            (<typeof Example>this.constructor).someProperty
        );
    }
}

class SubExample {
    static someProperty = "Overloaded! Hello world!";

    someMethod() {
        console.log(
            (<typeof SubExample>this.constructor).someProperty
        );
    }
}

After this proposal, the above block could be shortened to:

class Example {
    static someProperty = "Hello, world!";

    constructor() {
        // Output overloaded value of someProperty, if it is overloaded.
        console.log(
            this.constructor.someProperty
        );
    }
}

class SubExample {
    static someProperty = "Overloaded! Hello world!";

    someMethod() {
        console.log(
            this.constructor.someProperty
        );
    }
}

This removes a cast to the current class.

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:214
  • Comments:75 (17 by maintainers)

github_iconTop GitHub Comments

38reactions
hinellcommented, Nov 15, 2021

@thetutlage I think we all can safely forget about it and move on.

35reactions
thetutlagecommented, Nov 7, 2021

Any plans on picking this up. Seems like a real issue

Read more comments on GitHub >

github_iconTop Results From Across the Web

c++ - How to know a generic type T if it has an appropriate ...
Your code has a type T that should follow the concepts you are requiring. In your case, you want it to be default...
Read more >
Generic Constructors in Java | Baeldung
A generic constructor is a constructor that has at least one parameter of a generic type. We'll see that generic constructors don't have...
Read more >
constructor - JavaScript - MDN Web Docs - Mozilla
The constructor method is a special method of a class for creating and initializing an object instance of that class.
Read more >
Constraints on type parameters - C# Programming Guide
Because all value types have an accessible parameterless constructor, the struct constraint implies the new() constraint and can't be ...
Read more >
Documentation - Classes - TypeScript
There are just a few differences between class constructor signatures and function signatures: Constructors can't have type parameters - these belong on the ......
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