【发布时间】:2022-01-26 02:46:33
【问题描述】:
我有一个组件:
type Base = {
color: string
}
type Button = {
to: string
} & Base
type Link = {
link: string
linkNewTab: boolean
} & Base
type ComponentProps = Button | Link
export const Button: React.FC<ComponentProps> = (props) => {
return (<>
{props.link ? <Link newTab={props.linkNewTab}>...</Link> : props.to && <Button>...</Button>}
</>)
}
- 有一个带有 props 的基本类型,这两种类型都有共同点。
- 根据给定的道具,组件应该具有 Button 或 Link 类型。但如果它有链接,这些道具是首选。
打字稿错误:
Property 'link' does not exist on type 'PropsWithChildren<ComponentProps>'.
Property 'link' does not exist on type '{ to: string; } & Base & { children?: ReactNode; }'.ts(2339)
我不想要的:
- 我可以通过向 Base 类型添加类型并决定允许使用哪些道具来解决问题。我想根据道具自动决定。
信息:4.5.4 TypeScript Version
【问题讨论】:
标签: javascript reactjs typescript