-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Description
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)