【发布时间】:2019-02-27 20:18:26
【问题描述】:
我有一个简单的通用 React 组件,我并不总是知道我将使用哪种元素类型或道具,也不想为每个潜在道具设置一个接口。
使用 TS v2.9。
在这种情况下,我使用的是 React Native 而不是 React 的事实不应该是相关的。
import React, { StatelessComponent as SC } from "react";
interface IGenComponentProps {
/**
* Component type
*/
ComponentName: string;
/**
* Test Id used for automation testing
*/
testId?: string;
/**
* I'd like to define this here to supress TS errors I'm getting
*/
remainderProps?: any;
}
const GenComponent:SC<IGenComponentProps> = ({ComponentName, testId, children, ...remainderProps}) => {
return (
<ComponentName id={testId} {...remainderProps}>
{children}
</ComponentName>
)
}
export default GenComponent;
这很好用,符合预期,但在使用组件时出现 TS 错误:
<GenComponent
ComponentName={Image}
testId="test-image"
source={{uri: 'someImageSrc'}}
opacity={0.1}
/>
[ts] 类型'IntrinsicAttributes & IGenComponentProps & { children?: ReactNode; 上不存在属性'source' }'。
[ts] 属性“不透明度”不存在于类型“IntrinsicAttributes & IGenComponentProps & { children?: ReactNode; }'。
或:
<GenComponent
ComponentName={View}
testId="left-container"
accessibilityHint="left-container"
>
{ someContent }
</GenComponent>
[ts] 属性 'accessibilityHint' 不存在于类型'IntrinsicAttributes & IGenComponentProps & { children?: ReactNode; }'。
【问题讨论】:
标签: javascript reactjs typescript react-native typescript-typings