Typescript: Allow static members in abstract classes to reference type parameters

Created on 23 Oct 2019  路  7Comments  路  Source: microsoft/TypeScript

Search 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?

To quote @andy-ms in #24018:

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:

  • [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • [x] This wouldn't change the runtime behavior of existing JavaScript code
  • [x] This could be implemented without emitting different JS based on the types of the expressions
  • [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • [x] This feature would agree with the rest of TypeScript's Design Goals.
Awaiting More Feedback Suggestion

Most helpful comment

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.

abstract class ResourceService<T> {
    protected static resourcePath = 'override-me'

    static async fetch(id: string) {
        const url = `api/${this.resourcePath}/${id}`
        return getRequest<T>(url) // 'Static members cannot reference class type parameters.'
    }
}

class UserService extends ResourceService<User> {
    protected static resourcePath = 'users'

    // Additional functions specific to UserService
}

UserService.fetch('123') // GET api/users/123 -> Should be inferred as Promise<User>

All 7 comments

Duplicate of #32246, #32211, #24018 and others?

@j-oliveras those issues are related to using type parameters in _all_ classes (which is problematic). My suggestion is to allow them only in abstract classes, as they are meant to be inherited.

I think this is really putting the cart before the horse relative to #34516.

Anyway, A and B happen to be both concrete and non-generic, but they could vary on either axis without problem. Is the idea that this code would just be illegal because there's no legal parameter declaration for m ?

abstract class Super<T> {
    abstract static m(x: T): void;
}

class A<U> extends Super<U> {
    static m(x: __??__) {
        console.log(x * 42);
    }
}

I think this is really putting the cart before the horse

That's true, but pointing out possibilities that #34516 _could_ open will make it easier to determine it's value and whether it should be implemented, no?

Is the idea that this code would just be illegal because there's no legal parameter declaration for m?

I haven't thought about that case, but it makes sense, yes. After all, abstract classes allow for ambiguity. It's in their name. If you extend an abstract class and don't give concrete types for its parameters, there's still ambiguity left over, i.e. the resulting class should still be abstract.

So if you want to have a class with generics that extends an abstract class, it should be abstract itself. Your example should turn into:

abstract class Super<T> {
    abstract static m(x: T): void;
}

abstract class A<U> extends Super<U> {
    static m(x: U) {
        console.log(x * 42);
    }
}

...and then you use the resulting class:

class B extends A<number> {}

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 abstract Plugin 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's config argument, the non-static config member, and the static defaultConfig 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.

I also have a need for this.

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.

abstract class ResourceService<T> {
    protected static resourcePath = 'override-me'

    static async fetch(id: string) {
        const url = `api/${this.resourcePath}/${id}`
        return getRequest<T>(url) // 'Static members cannot reference class type parameters.'
    }
}

class UserService extends ResourceService<User> {
    protected static resourcePath = 'users'

    // Additional functions specific to UserService
}

UserService.fetch('123') // GET api/users/123 -> Should be inferred as Promise<User>
Was this page helpful?
0 / 5 - 0 ratings

Related issues

xealot picture xealot  路  150Comments

RyanCavanaugh picture RyanCavanaugh  路  205Comments

rwyborn picture rwyborn  路  210Comments

OliverJAsh picture OliverJAsh  路  242Comments

blakeembrey picture blakeembrey  路  171Comments