Во-первых, вы должны объявить, что MyButton
имеет более специфические реквизиты, поэтому MyComponent
должен быть параметризован:
class MyComponent<P extends IMyComponentProps> extends Component<P> {
getProps() {
return this.props
}
}
Затем правильно расширьте MyComponent
и объявите его реквизиты:
interface IMyButtonProps extends IMyComponentProps {
colWidth?: number;
title: string;
onPress: () => void;
}
class MyButton extends MyComponent<IMyButtonProps> {
render() {
return (
<View {...this.getProps()}>
<Button title={this.props.title} onPress={this.props.onPress} />
</View>
);
}
}
Тогда, если вы не используете ссылки, о которых трудно рассуждать, не расширяйте React.Props
. Просто объявите ваши интерфейсы так:
interface IMyComponentProps {
colWidth?: number;
}
interface IMyButtonProps extends IMyComponentProps {
title: string;
onPress: () => void;
}
Всего сейчас!
я
mport React, { Component } from "react";
import { Button, View } from "react-native";
interface IMyComponentProps {
colWidth?: number;
}
class MyComponent<P extends IMyComponentProps> extends Component<P> {
getProps() {
return this.props
}
}
interface IMyButtonProps extends IMyComponentProps {
title: string;
onPress: () => void;
}
class MyButton extends MyComponent<IMyButtonProps> {
render() {
return (
<View {...this.getProps()}>
<Button title={this.props.title} onPress={this.props.onPress} />
</View>
);
}
}