[react] Workaround for --strictFunctionTypes (#24709)

* [react] Workaround for --strictFunctionTypes

Under --strictFunctionTypes, when assigning a class with `getDerivedStateFromProps` to `React.ComponentClass` or `React.ComponentType`, the type of the second argument was expected to be `Readonly<any>`, which is, actually, the same as `Readonly<{}>`.

This was preventing using classes that try to refer to the previous state in `getDerivedStateFromProps` from being given to HOC factories.

There are no tests as testing this change is only possible with `--strictFunctionTypes`, and the `tsconfig.json` here specifically disables it. Perhaps another PR should enable it.

* Add a test anyway

Though it wouldn't have failed unless `--strictFunctionTypes` were enabled.

* Fix lint error on the test
This commit is contained in:
Diogo Franco
2018-04-04 14:10:25 +09:00
committed by John Reilly
parent b9cadc9a0f
commit 02ff115ce9
2 changed files with 2 additions and 1 deletions

View File

@@ -404,7 +404,7 @@ declare namespace React {
*
* Note: its presence prevents any of the deprecated lifecycle methods from being invoked
*/
(nextProps: Readonly<P>, prevState: Readonly<S>) => Partial<S> | null;
(nextProps: Readonly<P>, prevState: S) => Partial<S> | null;
// This should be "infer SS" but can't use it yet
interface NewLifecycle<P, S, SS> {

View File

@@ -156,6 +156,7 @@ class ComponentWithLargeState extends React.Component<{}, Record<'a'|'b'|'c', st
return { a: 'a' };
}
}
const AssignedComponentWithLargeState: React.ComponentClass = ComponentWithLargeState;
const componentWithBadLifecycle = new (class extends React.Component<{}, {}, number> {})({});
componentWithBadLifecycle.getSnapshotBeforeUpdate = () => { // $ExpectError