【问题标题】:Base constructors must all have the same return type基本构造函数必须都具有相同的返回类型
【发布时间】:2018-01-18 04:36:44
【问题描述】:

我想将 jsx 重写为 tsx。我有一个从 react-bootstrap 方法重写方法的代码:

import {Panel} from 'react-bootstrap';
class CustomPanel extends Panel {
    constructor(props, context) {
        super(props, context);
    }

    handleClickTitle = () => {
        return;
    }
}

Typescript 编译器抛出异常

 Base constructors must all have the same return type

如何解决?打字稿版本 2.3.4

【问题讨论】:

  • 什么会解决你的问题(但不是首先导致它)是完全省略构造函数。只调用super() 的构造函数是多余的。
  • 如果删除构造函数我有相同的类型加密错误 MyFile.tsx(2, 27): error TS2510: Base constructor must have the same return type

标签: reactjs typescript ecmascript-6 react-bootstrap


【解决方案1】:

用于 react-bootstrap 继承的肮脏技巧

const UntypedPanel = Panel as any;
class CustomPanel extends UntypedPanel {
    handleClickTitle = () => {
        return;
    }
}
const TypedCustomPanel = CustomPanel as any as React.ClassicComponentClass<PanelProps>;

【讨论】:

    【解决方案2】:

    这是我的解决方案:也觉得脏

    import { ComponentClass } from 'react';
    class CustomPanel extends (Panel as ComponentClass) {
       // others...
    

    【讨论】:

      猜你喜欢
      • 2022-11-08
      • 1970-01-01
      • 1970-01-01
      • 2015-06-05
      • 2010-09-07
      • 2013-07-10
      • 1970-01-01
      • 2014-11-30
      • 2019-12-24
      相关资源
      最近更新 更多