![](/img/trans.png)
[英]Typescript ESLint Error - How can I type children when using react functional components?
[英]Can I type React Children using Typescript?
目前无法以您想要的方式实现它。 不幸的是ReactElement
放松了类型检查,如下所述: 如何使用 TypeScript 2.3 中新添加的支持来限制 TypeScript 中的 React Children 的类型?
毕竟Reacts 组合使用道具将其传递给具有适当类型的组件:
import { ComponentPropsWithoutRef, PropsWithChildren } from "react";
type MessageCardProps = PropsWithChildren<{
icon: ComponentPropsWithoutRef<"svg">;
title: string;
}>;
function MessageCard({ icon, title, children }: MessageCardProps) {
return <div>{icon}{title}{children}</div>;
}
export default function App() {
return (
<MessageCard icon={<svg></svg>} title="title">
message
</MessageCard>
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.