question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

setState has wrong type

See original GitHub issue

We have interface:

interface State {
  condition: boolean;
  left: number;
  right: number;
}

and defined state:

  public state: State = {
    condition: true,
    left: 0,
    right: 0,
  };

Consider method:

  public change= (value: number) => {
    this.setState((state) => {
      if(state.condition) {
        return {
          left: value,
        }
      } else {
        return {
          right: value,
        }
      }
    });
  };

This cause an error during compilation.

TS:

...
        Type '{ left: number; right?: undefined; }' is not assignable to type 'Pick<State, "left" | "right">'.
          Types of property 'right' are incompatible.
            Type 'undefined' is not assignable to type 'number'.

Posible Solution:

       setState<K extends keyof S>(
            state: ((prevState: Readonly<S>, props: Readonly<P>) => (Partial<Pick<S, K>> | S | null)) | (Pick<S, K> | S | null),
            callback?: () => void
        ): void;

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
laxipcommented, Mar 7, 2019

Im not sure, maybe I should add this issue to DefinitelyTyped/DefinitelyTyped/tree/master/types/react

0reactions
awearycommented, Mar 7, 2019

The React team doesn’t maintain these TypeScript definitions. Please file this issue in the DefinitelyTyped repo. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

React .setState returns type error - Stack Overflow
The setState is a function, you change it to an JavaScript Object , I guess it's a tiny mistake, you have to write:...
Read more >
Why is setState giving me the wrong value? - Codetain
The first one shows what user types in the input field. The second one indicates what will be sent from our form. Right...
Read more >
React setState usage and gotchas - ITNEXT
A React class component has an internal state which like props affects how the component renders and behaves. Unlike props, state is local...
Read more >
How To Use and Not Use State - React Training
You just can't conditionalize the calls to useState because the call order matters to React. The number of times you called useState is...
Read more >
React.Component
These methods are called when there is an error during rendering, ... it should examine this.props and this.state and return one of the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found