原子设计打字稿反应

时间:2020-07-17 15:03:30

标签: reactjs typescript atomic-design

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组件开始,它检查是否存在要支持的属性以呈现默认的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

有什么想法吗?

0 个答案:

没有答案