且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

React Typescript:类型'{[x:number]的参数:任意; }'不能分配给类型的参数

更新时间:2023-11-12 20:24:46

您需要告诉Typescript您的对象将具有IAddPlayerFormState的一个或多个属性,但不一定具有所有属性.您可以这样做:

You need to tell Typescript that your object will have one or more property from IAddPlayerFormState, but not necessarily all properties. You can do it like this:

public onChange(event: { target: { name: any; value: any; }; }) {
  const newState = { [name]: value } as Pick<IAddPlayerFormState, keyof IAddPlayerFormState>;
  this.setState(newState);
  console.log("On Change!");
}