microsoft / microsoft/TypeScript
T.constructor should be of type T
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Use the two class examples in the issue as the behavioral starting point, then trace the TypeScript type-checking rules for instance constructors and static members. Done means the proposed this.constructor accesses type-check without the casts shown, while preserving the intended behavior for subclasses.
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
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100