类型"{}"缺少类型"消息"中的以下属性:



我在使用对象传播运算符时收到以下错误:

Type '{}' is missing the following properties from type 'Message':...

returnMessage已经被分配了一个类型,所以我无法弄清楚我应该怎么做?

我的代码:

dispatchMessages = (message: Message): void => {
this.setState({ messageFromDashboard: message });
const messageCopyFromState: Message = { ...this.state.messageFromDashboard };
let returnMessage: Message;
setTimeout(() => {
returnMessage = this.createMessageFromServer(messageCopyFromState);
this.setState({ messageToDashboard: returnMessage });
}, 1000);
}

提前致谢

组件状态的类型是什么?您可以尝试这样定义它:

type State = {
messageFromDashboard: Message
// your other state properties
}
type Props = any;
class MyComponent extends React.Component<Props, State> {
}

在 react-native 中,如果你想使用带有它类型的状态属性,那么首先你必须用你将要使用的每个属性声明状态检查下面的示例:

interface IState {
message: Message;
}
export class MyComponent extends React.PureComponent<IProps, IState> {

constructor(props : any) {
super(props);
this.state = {
message: new Message()
};
}

render() {
return <span>{`${this.props.superhero} health is: ${this.state.health}`}</span>
}
}

最新更新