Allow static members in abstract classes to reference type parameters
See original GitHub issueSearch Terms
abstract generic class static type parameter
Suggestion
It has been previously concluded in #24018 that referencing class type parameters in the static side is problematic unless that class is meant to be extended. Well, abstract classes are meant to be extended, so it makes sense to allow type parameters to be used?
Without inheritance that wouldn’t make much sense:
class Super<T> { static m(x: T): void; } Super.m(); // What's `T`?
This is a valid point. But with a solution for abstract static members from #34516, this could be refactored like so:
abstract class Super<T> {
abstract static m(x: T): void;
}
class A extends Super<number> {
static m(x) {
console.log(x * 42);
}
}
class B extends Super<string> {
static m(x) {
console.log(x + ' World!');
}
}
A.m(2);
B.m('Hello');
Use Cases
Pretty much everywhere that instance side types are related to static side types and where instance methods depend on static properties.
Examples
As an example I’ll give my personal use case. I have a class with a static defaults
property and instances merge it with a constructor argument of the same type but partial. Then, the resulting object is stored in an instance property:
abstract class Base<T> {
static defaults: T
config: T
constructor(options: Partial<T>) {
this.config = Object.assign({}, (this.constructor as typeof Base).defaults, options);
}
}
interface Options {
a: string
b: number
}
class A extends Base<Options> {
static defaults = {
a: 42, // Type '42' is not assignable to type 'string'.
b: 'oops' // Type '"oops"' is not assignable to type 'number'.
};
}
let inst = new A({
a: 'bar', // OK
b: 'baz' // Type '"baz"' is not assignable to type 'number'.
});
inst.config.a = 12; // Type '12' is not assignable to type 'string'.
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 4 years ago
- Reactions:26
- Comments:10 (2 by maintainers)
I would also love this feature to be added, but I don’t see why it should be limited to abstract members? As long as the super class is abstract, it should be sufficient. Consumption of inherited members is allowed through the non-abstract class.
In my use case I have a generic service for creating, updating, deleting and fetching resources through REST. This service is extended with specific services that modify the path used in the request (and also add their own requests), but TypeScript is unable to correctly type the response.
I’d appreciate this. I’m running into this problem when I try to declare a type parameter for the shape of a config object for plugins in a plugin system I’ve created for a Discord bot project.
Right now, I have declared type
any
because different plugins (classes extending from an abstractPlugin
class) use different config objects - they’re not standardized because different plugins do different things, so a universal interface wouldn’t be the right way to go. A type parameter would end up being used for the constructor’sconfig
argument, the non-staticconfig
member, and the staticdefaultConfig
member (used to define the minimum working config).It would be great to get my linter off my back the right way by providing actual types instead of shutting it up with rule-ignore comments.