Definitelytyped: React 16.6 types

Created on 24 Oct 2018  路  5Comments  路  Source: DefinitelyTyped/DefinitelyTyped

React 16.6 was released today: https://reactjs.org/blog/2018/10/23/react-v-16-6.html

I would have made this as a PR, but there is a problem with the new static contextType feature that I don't know how to solve. This is probably a TypeScript core problem.

First, the type of context is defined in class Component and marked as @deprecated (which triggers the deprecation tslint rule), but context is only deprecated if static contextType is not defined.

Second, the type of this.context depends on the type of static contextType, and there is no way I can think of, within TypeScript's generic system, declaring that in the types. While I can hack that into ComponentClass's definition, it wouldn't help actually subclassing Component correctly.

Probably the only solution that would work short term is removing the context: any declaration entirely, but it'd be a breaking change.


This is not the first time we hit limitations of the generic system in React; see getDerivedStateFromProps which had to be left out of Component's definition entirely because it'd just have to have any inputs and outputs anyway.

Most helpful comment

What version of @types/react does contain 16.6 features but doesn't contain 16.7 features? There is no 16.6 version of @types/react in NPM.

All 5 comments

@Kovensky you can use something like that for getDerivedStateFromProps:

export class Comp<P> extends React.Component<Props<P>> {
  public static getDerivedStateFromProps<P>(
    ...
  ) {
    ...
  }
}

The problem is that that P is just a hidden any. It is _shadowing_ the P parameter of the class by creating its own local P parameter.

Wow, bad news for me( Thank you for explanation.

What version of @types/react does contain 16.6 features but doesn't contain 16.7 features? There is no 16.6 version of @types/react in NPM.

I'm looking at our company package.json and it's unexpectedly hard to install/update @types packages that matches the actual packages. I hope this change some day!

for now, any help on this issue would be great :)

Was this page helpful?
0 / 5 - 0 ratings