diff --git a/types/react/index.d.ts b/types/react/index.d.ts index d9c4418b82..c5c1c76d17 100644 --- a/types/react/index.d.ts +++ b/types/react/index.d.ts @@ -282,8 +282,9 @@ declare namespace React { // We MUST keep setState() as a unified signature because it allows proper checking of the method return type. // 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 & Partial)) | (Pick & Partial), + state: ((prevState: Readonly, props: P) => (Pick | S)) | (Pick | S), callback?: () => any ): void; diff --git a/types/react/test/tsx.tsx b/types/react/test/tsx.tsx index e43887c453..38d902c387 100644 --- a/types/react/test/tsx.tsx +++ b/types/react/test/tsx.tsx @@ -78,7 +78,8 @@ const StatelessComponentWithoutProps: React.SFC = (props) => { ; -class Comp extends React.Component<{}, { foo: boolean, bar: boolean }> { +// Below tests that setState() works properly for both regular and callback modes +class SetStateTest extends React.Component<{}, { foo: boolean, bar: boolean }> { handleSomething = () => { this.setState({ foo: '' }); // $ExpectError this.setState({ foo: true }); @@ -94,3 +95,17 @@ class Comp extends React.Component<{}, { foo: boolean, bar: boolean }> { this.setState({ foo: true, bar: undefined}); // $ExpectError } } + +// Below tests that extended types for state work +export abstract class SetStateTestForExtendsState extends React.Component { + foo() { + this.setState({ baseProp: 'foobar' }); + } +} + +// Below tests that & generic still works +export abstract class SetStateTestForAndedState extends React.Component { + foo() { + this.setState({ baseProp: 'foobar' }); + } +} diff --git a/types/react/v15/index.d.ts b/types/react/v15/index.d.ts index 7e036997fb..12b25439aa 100644 --- a/types/react/v15/index.d.ts +++ b/types/react/v15/index.d.ts @@ -284,8 +284,9 @@ declare namespace React { // We MUST keep setState() as a unified signature because it allows proper checking of the method return type. // 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 & Partial)) | (Pick & Partial), + state: ((prevState: Readonly, props: P) => (Pick | S)) | (Pick | S), callback?: () => any ): void; diff --git a/types/react/v15/test/tsx.tsx b/types/react/v15/test/tsx.tsx index 6f72279ff1..8afb75d9a8 100644 --- a/types/react/v15/test/tsx.tsx +++ b/types/react/v15/test/tsx.tsx @@ -64,7 +64,8 @@ const StatelessComponentWithoutProps: React.SFC = (props) => { }; ; -class Comp extends React.Component<{}, { foo: boolean, bar: boolean }> { +// Below tests that setState() works properly for both regular and callback modes +class SetStateTest extends React.Component<{}, { foo: boolean, bar: boolean }> { handleSomething = () => { this.setState({ foo: '' }); // $ExpectError this.setState({ foo: true }); @@ -80,3 +81,17 @@ class Comp extends React.Component<{}, { foo: boolean, bar: boolean }> { this.setState({ foo: true, bar: undefined}); // $ExpectError } } + +// Below tests that extended types for state work +export abstract class SetStateTestForExtendsState extends React.Component { + foo() { + this.setState({ baseProp: 'foobar' }); + } +} + +// Below tests that & generic still works +export abstract class SetStateTestForAndedState extends React.Component { + foo() { + this.setState({ baseProp: 'foobar' }); + } +}