【发布时间】:2019-01-20 23:13:44
【问题描述】:
我有一个 React Wrapper 组件,它接受一些道具,但将所有其他道具转发给子组件(尤其是与 className、id 等原生道具相关)。
但是,当我传递原生道具时,Typescript 会抱怨。查看错误信息:
TS2339:类型上不存在属性“className” 'IntrinsicAttributes & IntrinsicClassAttributes & Readonly & 只读'。
我怎样才能得到一个带有特定 props 的组件也接受原生 props( 不接受任何 props 并放弃类型检查)?
我的代码如下所示:
interface WrapperProps extends JSX.IntrinsicAttributes {
callback?: Function
}
export class Wrapper extends React.Component<WrapperProps>{
render() {
const { callback, children, ...rest } = this.props;
return <div {...rest}>
{children}
</div>;
}
}
export const Test = () => {
return <Wrapper className="test">Hi there</Wrapper>
}
仅供参考:我在这里发现了一个类似的问题,但答案基本上放弃了类型检查,我想避免这种情况:Link to SO-Question
【问题讨论】:
标签: javascript reactjs typescript react-props