如何在TypeScript中扩展具有成分的React组件而不会“缺少属性”?

时间:2019-02-08 08:03:39

标签: reactjs typescript

这就是我要尝试的:

const ActionLink: typeof NavLink = ({onClick, ...props}) => {
    function handleClick(ev: React.MouseEvent<HTMLAnchorElement>) {
        ev.preventDefault();
        if(onClick) onClick(ev);
    }
    return <NavLink {...props} onClick={handleClick}/>
}

我认为NavLink只是我想要ActionLink'继承'其所有属性并不重要。但是,当我尝试这样做typeof NavLink时,出现此错误:

src/components/App.tsx:155:7 - error TS2739: Type '({ onClick, ...props }: ClassAttributes<HTMLAnchorElement> & AnchorHTMLAttributes<HTMLAnchorElement> & Pick<DetailedHTMLProps<AnchorHTMLAttributes<HTMLAnchorElement>, HTMLAnchorElement>, "media" | ... 257 more ... | "css"> & { ...; } & { ...; }) => Element' is missing the following properties from type 'StyledComponent<DetailedHTMLProps<AnchorHTMLAttributes<HTMLAnchorElement>, HTMLAnchorElement>, Pick<DetailedHTMLProps<AnchorHTMLAttributes<HTMLAnchorElement>, HTMLAnchorElement>, "media" | ... 257 more ... | "css">, any>': withComponent, __emotion_styles

155 const ActionLink: typeof NavLink = ({onClick, ...props}) => {
          ~~~~~~~~~~

为什么?我如何缺少物业? ...props是否应该全部捕获?


NavLinkemotion styled component

const NavLink = styled.a`
    display: flex;
    align-items: center;
    text-decoration: none;
    padding: 2px 5px;
    &:hover {
        background-color: #eee;
    }
`;
NavLink.defaultProps = {href: ''};

0 个答案:

没有答案