import React from "react"
import styled, { css } from "styled-components"
import * as Router from "react-router-dom"
const styles = css`
text-decoration: none;
font-weight: 500;
color: #0b0080;
&:hover {
text-decoration: underline;
}
`
const StyledLink = styled(({ ...props }) => <Router.Link {...props} />)`
${styles};
`
const Anchor = styled.a`
${styles};
`
type LinkProps = {
to: string | null
href: string | null
target?: string
}
export const Link: React.FunctionComponent<LinkProps> = ({ ...props }) => {
const { to } = props
if (to) {
return <StyledLink {...props} />
}
return <Anchor {...props} />
}
Link.defaultProps = {
to: null,
href: null,
}
我正在尝试为我的项目使用原子设计并且我从我的 Link 组件开始它检查是否有一个 to 支持它呈现一个默认StyledLink
组件但似乎我从Anchor
组件中收到此错误
Overload 2 of 2, '(props: StyledComponentPropsWithAs<"a", any, {}, never>): ReactElement<StyledComponentPropsWithAs<"a", any, {}, never>, string | ... 1 more ... | (new (props: any) => Component<...>)>', gave the following error. Type '{ to: string | null; href: string | null; target?: string | undefined; children?: ReactNode; }' is not assignable to type 'Pick<Pick<Pick<DetailedHTMLProps<AnchorHTMLAttributes<HTMLAnchorElement>, HTMLAnchorElement>, "style" | ... 257 more ... | "rel"> & { ...; }, "ref" | ... 258 more ... | "rel"> & Partial<...>, "ref" | ... 258 more ... | "rel">'. Types of property 'href' are incompatible. Type 'string | null' is not assignable to type 'string | undefined'. Type 'null' is not assignable to type 'string | undefined'. TS2769
有什么问题吗?