mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-06-06 06:19:58 +08:00
[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:
committed by
John Reilly
parent
b9cadc9a0f
commit
02ff115ce9
2
types/react/index.d.ts
vendored
2
types/react/index.d.ts
vendored
@@ -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> {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user