样式的组件隐藏了Typescript中的HTML属性
我创建了一个标准样式组件,例如:
export type ContainerProps = {
color: string;
};
export const Container = styled.div<ContainerProps>`
color: ${p => p.color};
background: blue;
width: 100%
`;
当我想在JSX中使用此组件时,我具有所有HTML属性作为Props。
有什么方法可以限制这些方法,以便用户只能使用道具类型中指定的道具?
I create a standard Styled Component like:
export type ContainerProps = {
color: string;
};
export const Container = styled.div<ContainerProps>`
color: ${p => p.color};
background: blue;
width: 100%
`;
When I want to use this component in JSX I have all the HTML attributes available as props.
Is there any way how to restrict these, so that the user can use only props specified in props type?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑您是否可以实现它,请检查实现在这里
可以创建另一个组件并导出此组件。
SC添加了额外的默认HTML Props(OnClick,Onblur等),如果我们提供FC,它将仅接受类型的道具。
I doubt you will be able to achieve it, check the implementation here
A naive approach can be, to create another component and export this.
SC adds extra default HTML props(onClick, onBlur etc), If we provide FC it'll only accept Type props.