无法在typescript中调用构造函数()中的super()

时间:2017-12-25 17:03:17

标签: reactjs typescript create-react-app

我正在尝试使用create-react-app --scripts-version = react-scripts-ts命令编写带有打字稿支持的反应应用程序。

我一直看到这个错误:

  

(23,15):错误TS2345:'Props |类型的参数undefined'不能分配给'Props'类型的参数。         类型'undefined'不能分配给'Props'类型。

这来自的代码是:

    let mailComposer = MFMailComposeViewController()
    mailComposer.mailComposeDelegate = self

    //Set to recipients
    mailComposer.setToRecipients(["your email address heres"])

    //Set the subject
    mailComposer.setSubject("email with document pdf")
    //set mail body
    mailComposer.setMessageBody("This is what they sound like.", isHTML: true)
    if let filePath = Bundle.main.path(forResource: "All_about_tax", ofType: "pdf")
    {
        print("File path loaded.")
        if let fileData = NSData(contentsOfFile: filePath)
        {
            print("File data loaded.")
            mailComposer.addAttachmentData(fileData, mimeType: "application/pdf", fileName: "All_about_tax.pdf")
        }
    }
    //this will compose and present mail to user
    self.present(mailComposer, animated: true, completion: nil)

不确定是什么造成了这种情况 - 我一直在搜索并且没有太多关于如何解决这个问题的线索?

更新

如果我删除了可选项?从构造函数签名,然后我开始看到这个错误:

export class ScratchpadComponent extends React.Component<ScratchpadComponent.Props, ScratchpadComponent.State> {

    constructor(props?: ScratchpadComponent.Props, context?: any) {
        super(props, context);
        this.submit = this.submit.bind(this);
    }

1 个答案:

答案 0 :(得分:2)

超类构造函数将Props作为其第一个参数。可以不带任何参数调用子类构造函数,因为您将其声明为

constructor(props?: ScratchpadComponent.Props, context?: any)

而不是

constructor(props: ScratchpadComponent.Props, context?: any)

因此,如果调用者没有传递任何参数,那么props是未定义的,并且你试图将undefined传递给超级构造函数,而超级构造函数不接受未定义。

相关问题