0

I am writing a component

Text
that accepts
size
prop based on which a Styled component could be returned. I am using the polymorphic
as
prop from Styled Components to do so. However, when TypeScript keeps throwing these errors. I am also using transient props to prevent passing the props intended for StyledText to the ReactNode.

For

<StyledText>
component

This JSX tag's 'children' prop expects type 'never' which requires multiple children, but only a single child was provided.ts(2745)

And for

as
prop in Styled Components

No overload matches this call.
  Overload 1 of 2, '(props: Pick<Pick<Pick<DetailedHTMLProps<HTMLAttributes<HTMLSpanElement>, HTMLSpanElement>, "color" | "slot" | ... 252 more ... | "onTransitionEndCapture"> & { ...; } & StyledTextProps, "color" | ... 259 more ... | "align"> & Partial<...>, "color" | ... 259 more ... | "align"> & { ...; } & { ...; }): ReactElement<...>', gave the following error.
    Type 'string' is not assignable to type 'never'.ts(2769)

I have created a simplified example in Sandbox

Any pointers on what am I doing wrong or recommended reading is highly appreciated.

Also pasting the code below:

			
import React from "react";
import styled from "styled-components";

export interface TextProps {
  color?: "primary" | "secondary" | "primaryText" | "secondaryText" | "error";
  size?: "1" | "2" | "3" | "4" | "5" | "6" | "overline";
  weight?: "normal" | "bold" | "bolder" | "lighter" | "number";
  display?: "inline" | "block" | "inherit";
  align?: "left" | "right" | "center" | "justify" | "initial" | "inherit";
  children?: React.ReactNode;
}

interface StyledTextProps {
  color?: TextProps["color"];size?: TextProps["size"];
  weight?: TextProps["weight"];display?: TextProps["display"];
  align?: TextProps["align"]; }  const StyledText = styled.span<StyledTextProps>`   font-family: var(--default-font-family);   margin: 0;   display:{(props) => (props.display ? props.display : `inherit`)};
  text-align: {(props) => (props.align ? props.align : `inherit`)};   font-weight:{(props) => (props.weight ? props.weight : `normal`)};
`;

const sizeToHeading: Record<string, string> = {
  "0": "p",
  "1": "h1",
  "2": "h2",
  "3": "h3",
  "4": "h4",
  "5": "h5",
  "6": "h6"
};

export const Text = React.forwardRef<HTMLSpanElement, TextProps>(
  ({ ...props }, ref) => {
    return (
      <>
        <StyledText
          ref={ref}
          $size={props.size || "6"}
          as={props.size ? sizeToHeading[props.size] : "p"}
        >
          {props.children}
        </StyledText>
      </>
    );
  }
);

Anonymous Asked question May 14, 2021