类型“ {}”缺少类型“消息”的以下属性:

时间:2019-10-10 13:04:04

标签: reactjs typescript

使用对象传播运算符时出现以下错误:

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);
  }

预先感谢

2 个答案:

答案 0 :(得分:0)

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

type State = {
  messageFromDashboard: Message
  // your other state properties
}

type Props = any;

class MyComponent extends React.Component<Props, State> {

}

答案 1 :(得分:0)

在react-native中,如果您想使用具有其类型的state属性,那么首先必须声明其状态的每个属性,并使用下面的检查示例:

fun drawOnContext()
    context?.apply{
        beginPath()
        onDraw(this)
        fillStyle = this@CanvasElement.fillStyle
        strokeStyle = this@CanvasElement.strokeStyle
        shadowColor = this@CanvasElement.shadowColor
        save()
        stroke()
        fill()
    }
}

abstract protected fun onDraw(context: Context)