无法在打字稿中调用 constructor() 内的 super()

Unable to call super() inside constructor() in typescript

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

我一直看到这个错误:

(23,15): error TS2345: Argument of type 'Props | undefined' is not assignable to parameter of type 'Props'. Type 'undefined' is not assignable to type 'Props'.

这来自的代码是这样的:

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

不确定是什么原因造成的 - 我四处搜索但没有太多关于如何解决这个问题的线索?

更新:

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

18,11): error TS2322: Type '{}' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<Component<Pick<Props, "dispatch" | "scratchPadActi...'.
  Type '{}' is not assignable to type 'Readonly<Pick<Props, "dispatch" | "scratchPadActions" | "scratchData" | "errorMessage" | "errorDa...'.
    Property 'dispatch' is missing in type '{}'.

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

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

而不是

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

因此,如果调用者未传递任何参数,则 props 是未定义的,而您正试图将 undefined 传递给不接受 undefined 的超级构造函数。