ค้นหาเว็บไซต์

reactjs - ตัวสร้างพื้นฐานทั้งหมดต้องมีประเภทค่าที่ส่งคืนเหมือนกัน


ฉันต้องการเขียน 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

จะแก้ไขมันอย่างไร? เวอร์ชัน Typescript 2.3.4

คำตอบ #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...