I'm getting an overload typescript warning when using styled components as prop with a custom prop.
Overload 1 of 2, '(props: Omit & Partial>, "theme"> & { ...; } & { ...; }): ReactElement', gave the following error.
Type 'string' is not assignable to type 'undefined'.
Overload 2 of 2, '(props: StyledComponentPropsWithAs):
ReactElement, string | JSXElementConstructor>', gave the following error.
Type '{ children: string | undefined; as: "span"; variants: { show: { opacity: number; y: number; transition: { ease: string; duration: number; staggerChildren: number; delayChildren: number; }; }; hide: { ...; }; }; }' is not assignable to type 'IntrinsicAttributes & Omit, HTMLSpanElement>, "key" | keyof HTMLAttributes> & { ...; }, never> & Partial, "theme"> & { ...; } & { ...; }'.
Property 'variants' does not exist on type 'IntrinsicAttributes & Omit, HTMLSpanElement>, "key" | keyof HTMLAttributes> & { ...; }, never> & Partial, "theme"> & { ...; } & { ...; }'.ts(2769)
index.d.ts(161, 53): The expected type comes from property 'as' which is declared here on type 'IntrinsicAttributes & Omit & Partial>, "theme"> & { ...; } & { ...; }'
Example:
Link component
import React, { FC } from "react";
import styled from "styled-components";
import { motion } from "framer-motion";
interface IProps {
variants?: any;
}
const Atag= styled(motion.a)`
text-decoration: none;
`;
const Link: FC<IProps> = (props) => {
return <Atag{...props} />;
};
export default Link;
Another file where I import in Link.
const StyledLink= styled(Link)`
border: 1px solid red;
`;
<StyledLink as="button" variants={motionVariant}>hello</StyledLink>
motionVariant is an object for frame motion.
If I remove the variants prop and just use the as prop typescript stops complaining. If I remove the as prop and keep the variants prop it stops complaining. But it will not let me use the two together. Why can't I use both here and still keep typescript happy.