Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Infer Prop interface from styled-component

I try to infer my components' Props interface over exporting them whenever possible. This isn't an issue with class and functional components, but if I try to infer the Props interface of a styled-component, the prop is typed to any which is not ideal.

interface Props {
  bgColor: string;
  children: React.ReactNode;
}

const Box = styled.div<Props>`
  background-color: ${(p) => p.bgColor};
`;

const Button = (props: Props) => (
  <button style={{ backgroundColor: props.bgColor }}>{props.children}</button>
);

type ButtonInferredProps = React.ComponentProps<typeof Button>;

type BoxInferredProps = React.ComponentProps<typeof Box>;

const OtherBox = (props: BoxInferredProps) => (
  <div style={{ backgroundColor: props.bgColor }}>{props.children}</div>
);

const OtherButton = (props: ButtonInferredProps) => (
  <button style={{ backgroundColor: props.bgColor }}>{props.children}</button>
);

export default function App() {
  return (
    <>
      <Box bgColor="red">Hi! I'm a box! </Box>
      <OtherBox bgColor="purple" backgroundColor="red">
        Hi! I'm another box
      </OtherBox>
      <Button bgColor="blue">Hi! I'm a button</Button>
      <OtherButton bgColor="green">Hi! I'm another button</OtherButton>
    </>
  );
}

With Box being a styled-component, I can't properly infer its Props interface. When I create another component that uses attempts to use the inferred Props type, it comes through as any:

const OtherBox = (props: BoxInferredProps) => (
  {/* TS won't complain that `props` doesn't have a `iAmNotTyped` property which is desired... */}
  <div style={{ backgroundColor: props.iAmNotTyped}}>{props.children}</div>
);

https://codesandbox.io/s/styled-components-typescript-forked-7cq4q?file=/src/App.tsx

like image 713
Mister Epic Avatar asked Dec 31 '22 20:12

Mister Epic


2 Answers

styled-components exports a Typescript helper for this.

It's called StyledComponentPropsWithRef:

import React from 'react';
import styled, { StyledComponentPropsWithRef } from 'styled-components';

const RedLink = styled.a`
  color: red;
`;

type Props = StyledComponentPropsWithRef<typeof RedLink>;

const Link = (props: Props) => <RedLink {...props} />;

export default function App() {
  return <Link href="/" role="button" />;
}

This code works ☝️

like image 98
Ramon Balthazar Avatar answered Jan 05 '23 17:01

Ramon Balthazar


The type for your Box styled component is showing up as StyledComponent<"div", any, Props, never>. So you can make use of typescript's infer keyword to extract Props from that type definition.

import {StyledComponent} from "styled-components";

type StyledComponentProps<T> = T extends StyledComponent<any, any, infer P, any> ? P : never

type BoxInferredProps = StyledComponentProps<typeof Box>;

Now you get a typescript error, as expected, when trying to assign backgroundColor on OtherBox.

like image 40
Linda Paiste Avatar answered Jan 05 '23 17:01

Linda Paiste