【发布时间】:2020-07-19 20:50:03
【问题描述】:
我正在使用带有 Typescript 的 NativeBase(包名无关紧要)。
我正在创建一个包装 NativeBase TextInput 的简单输入组件。我添加了一些自己的属性并将所有其他属性传播到 NativeBase TextInput。 这意味着 props 对象应该包含我的自定义属性和 NativeBase TextInput。
我的问题是:如何在不复制 NativePase 属性的情况下通过打字稿正确描述它?
import { Input } from 'native-base';
type Props = {
// my custom prop
style: object;
// props from NativeBase
onFocus?: () => void;
onBlur?: () => void;
}
export class TextInput extends Component<Props, State> {
render() {
// without these callbacks in Props typescript will complain
const { style, onFocus, onBlur, ...otherProps } = this.props;
return (
<Input style={this.getStyle()} {...otherProps}/>
);
}
}
我尝试使用 type intersection 但它不起作用,因为 Input 通常不是“类型”;
type Props = Input & {
// my custom prop
style: object;
}
另外,我尝试通过 typeof 提取输入类型。没有帮助。
type Props = typeof Input & {
// my custom prop
style: object;
}
那么有没有一种方法可以避免复制粘贴我想使用的包可能的道具?
感谢您的帮助!
【问题讨论】:
标签: reactjs typescript react-native types native-base