javascript - ขยายคลาส TypeScript ด้วยพารามิเตอร์ทั่วไปหลายตัว
ฉันพยายามที่จะขยายคลาส React Component ด้วยพารามิเตอร์แบบทั่วไปแต่ฉันไม่สามารถทำให้มันทำงานได้ ตามเอกสารของ TypeScript และข้อจำกัดของพารามิเตอร์คลาสแบบทั่วไปของ TypeScript และการขยายคลาสด้วย generic T ฉันจบลงด้วยโค้ดดังต่อไปนี้ (ซึ่งไม่ทำงาน):
คลาสฐาน:
class BaseView<P, S> extends React.Component<P, S> {
public constructor(props: P, state: S) {
super(props, state);
}
}
const mapStateToProps = store => {
console.log('store', store);
return {
store
};
};
function mapDispatchToProps(dispatch) {
console.log('dispatch', dispatch);
return {
};
}
export default connect(mapStateToProps, mapDispatchToProps)((BaseView as any));
export function subclass<P, S>(c): BaseView<P, S> {
return c;
}คลาสที่สืบทอดจากคลาสฐาน:
ฉันลองหลายวิธี โดยใช้โรงงานแบบนี้:
export class Documents extends subclass(BaseView)<DocumentsProperties, DocumentsState> {
// this leads into ""BaseView<{}, {}>" is no constructor function type
หรือต้องไม่ใช้โรงงาน:
export class Documents extends BaseView<DocumentsProperties, DocumentsState> {
// no base constructor has the specified number of type arguments
ฉันก็ลองวิธีอื่นหลายอย่างเหมือนกัน แต่รู้สึกว่าฉันกำลังเขียนโปรแกรมเป็นครั้งแรกในชีวิต...ฉันไม่สามารถทำให้มันรันได้สำหรับหลายพารามิเตอร์ ฉันพลาดอะไรไปตรงนี้หรือ?
สวัสดี Messerbill