Typescript generic state change function type casting error

后端 未结 2 1890
后悔当初
后悔当初 2021-01-24 03:52

I\'m creating a generic function to change the state.

The error message:

TS2345: Argument of type \'() => { [x:string]: string }\' is not assignable          


        
2条回答
  •  遥遥无期
    2021-01-24 04:03

    There are a few solutions:

     this.setState({ [name]: value } as Partial)
    

    Or

     this.setState({ [name]: value } as Pick);
    

    There is an open issue in git, take a look: cannot setState with dynamic key name type-safe

提交回复
热议问题