Skip to content

Allow static members to reference class type parameters #24018

@jaredru

Description

@jaredru

Search Terms

static, generic, type, getDerivedPropsFromState

Suggestion

Static class members should be allowed to reference the class's type parameters.

Use Cases

React 16.3 introduced a static lifecycle method to Component. Its definition should look something like:

class Component<P, S> {
  static getDerivedStateFromProps?(p: P, s: S): Partial<S> | null;
}

However, that definition results in compiler error TS2302 "Static members cannot reference class type parameters", so it doesn't exist on the DefinitelyTyped definition for Component. In practice, this means we get less help from the editor in the form of code completion, and we get less type safety, as it's possible to misdefine the types when implementing getDerivedStateFromProps.

FWIW, Flow supports this.

Examples

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. new expression-level syntax)

Metadata

Metadata

Assignees

No one assigned

    Labels

    DeclinedThe issue was declined as something which matches the TypeScript vision

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions