From 02ff115ce9fdf39cf1ff998c761515b9acc79d42 Mon Sep 17 00:00:00 2001 From: Diogo Franco Date: Wed, 4 Apr 2018 14:10:25 +0900 Subject: [PATCH] [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`, 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 --- types/react/index.d.ts | 2 +- types/react/test/tsx.tsx | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/types/react/index.d.ts b/types/react/index.d.ts index 49224d93cf..2ed2fb497c 100644 --- a/types/react/index.d.ts +++ b/types/react/index.d.ts @@ -404,7 +404,7 @@ declare namespace React { * * Note: its presence prevents any of the deprecated lifecycle methods from being invoked */ - (nextProps: Readonly

, prevState: Readonly) => Partial | null; + (nextProps: Readonly

, prevState: S) => Partial | null; // This should be "infer SS" but can't use it yet interface NewLifecycle { diff --git a/types/react/test/tsx.tsx b/types/react/test/tsx.tsx index b086e35f9f..760a625cfd 100644 --- a/types/react/test/tsx.tsx +++ b/types/react/test/tsx.tsx @@ -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