T.constructor should be of type T
See original GitHub issueGiven
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:
- Created 8 years ago
- Reactions:214
- Comments:75 (17 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@thetutlage I think we all can safely forget about it and move on.
Any plans on picking this up. Seems like a real issue