Skip to content

Pick<> not working in React setState with updater function #16923

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Suzii opened this issue Jul 4, 2017 · 4 comments
Closed

Pick<> not working in React setState with updater function #16923

Suzii opened this issue Jul 4, 2017 · 4 comments
Labels
External Relates to another program, environment, or user action which we cannot control.

Comments

@Suzii
Copy link

Suzii commented Jul 4, 2017

TypeScript Version: 2.4.0 / nightly (2.5.0-dev.201xxxxx)
The React setState(...) function fails to detect incompatible value being returned from an updater function. Pick<> does not seem to work well in this case. I thought this would be fixed in v2.4 but it is not.

The issue migh be related to this one #13359 but it does not seem to be limited to conditional updates.

Code

class MyComponent extends React.PureComponent<{}, { value: string }> {
  _onChange = (event: ChangeEvent<HTMLInputElement>) => {
    const value = event.target.value;

    this.setState({ valueWithTypo: value }); // correctly reports an error
    this.setState(() => ({ valueWithTypo: value })); // does not detect the typo
  };

  render() {
    // some implementation
  }
}

Expected behavior:
Compiler detects wrong return type of the updater function passed to this.setState(...). (Overload which accepts object (not the updater function) reports an error correctly.)

Actual behavior:
Compiler fails to detect wrong return type of the updater function passed to this.setState(...). No error reported even though argument { valueWithTypo: value } is not compatible with state type { value: string }.

@Suzii Suzii changed the title Typecheck not working in React setState with updater function Pick<> not working in React setState with updater function Jul 4, 2017
@sylvanaar
Copy link

sylvanaar commented Jul 4, 2017

I have reduced your problem down to the typing that is causing the problem.

let f:Pick< { value: string }, never> = () => { return { a:1 }}
`

@dakom
Copy link

dakom commented Jul 11, 2017

On a related note, it seems the "broken" version here was the best fix for setting via a dynamic index.

Consider this snippet, modelled after the "Multiple Inputs" section at https://facebook.github.io/react/docs/forms.html but with TS where state satisfies a particular interface:

onFormUpdated(evt:React.ChangeEvent<HTMLInputElement>) {
    let name = evt.currentTarget.getAttribute("name");
    let value = evt.currentTarget.value;
    this.setState(() => ({ [name]: value}));
}

Will this break if the issue here is fixed? Is there a cleaner way to write it?

@mhegazy mhegazy added the Needs Investigation This issue needs a team member to investigate its status. label Aug 29, 2017
@danthegoodman
Copy link

There's been some discussion (but no resolution) of this over in DefinitelyTyped/DefinitelyTyped#18365

@RyanCavanaugh RyanCavanaugh added External Relates to another program, environment, or user action which we cannot control. and removed Needs Investigation This issue needs a team member to investigate its status. labels Sep 18, 2019
@typescript-bot
Copy link
Collaborator

This issue has been marked as 'External' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
External Relates to another program, environment, or user action which we cannot control.
Projects
None yet
Development

No branches or pull requests

7 participants