diff --git a/types/react/index.d.ts b/types/react/index.d.ts index 93b143b7bf..05398e49b4 100644 --- a/types/react/index.d.ts +++ b/types/react/index.d.ts @@ -284,7 +284,7 @@ declare namespace React { // See: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/18365#issuecomment-351013257 // Also, the ` | S` allows intellisense to not be dumbisense setState( - state: ((prevState: Readonly, props: P) => (Pick | S)) | (Pick | S), + state: ((prevState: Readonly, props: P) => (Pick | S | null)) | (Pick | S | null), callback?: () => void ): void; diff --git a/types/react/test/tsx.tsx b/types/react/test/tsx.tsx index bc48add9a4..4ad5a3e2c8 100644 --- a/types/react/test/tsx.tsx +++ b/types/react/test/tsx.tsx @@ -95,6 +95,7 @@ class SetStateTest extends React.Component<{}, { foo: boolean, bar: boolean }> { this.setState({ foo: true }); this.setState({ foo: true, bar: true }); this.setState({}); + this.setState(null); this.setState({ foo: true, foo2: true }); // $ExpectError this.setState(() => ({ foo: '' })); // $ExpectError this.setState(() => ({ foo: true })); @@ -103,6 +104,7 @@ class SetStateTest extends React.Component<{}, { foo: boolean, bar: boolean }> { this.setState(() => ({ foo: '', foo2: true })); // $ExpectError this.setState(() => ({ })); // ok! this.setState({ foo: true, bar: undefined}); // $ExpectError + this.setState(prevState => (prevState.bar ? { bar: false } : null)); } }