stitches icon indicating copy to clipboard operation
stitches copied to clipboard

TS error when using `as` + variant with composed components

Open peduarte opened this issue 4 years ago • 9 comments

To repro:

const Box = styled('div', {
  variants: {
    size: {
      small: {}
    }
  }
});
const Flex = styled(Box, {})

export function Component() {
  return <Flex as="article" size="small" />;
}

peduarte avatar Aug 30 '21 20:08 peduarte

Replication @ https://codesandbox.io/s/ecstatic-mayer-lobwy?file=/src/App.tsx

jonathantneal avatar Sep 09 '21 14:09 jonathantneal

If you want to update Stitches to the latest version but you're running into this issue... you can put // @ts-expect-error above the prop that's throwing the error and you can continue with your project.

janhoogeveen avatar Sep 22 '21 14:09 janhoogeveen

maybe this is coming from the as prop types as never in the styled-component.d.ts . I guess it could be type as keyof JSX.IntrinsicElements or something like that

KirdesMF avatar Oct 06 '21 02:10 KirdesMF

I have a scenario somewhat related:

const Box = styled('div', {
  color: 'red'
});

type MyBoxProps = {
  // --- how do I get the `as` prop type here? --- //
}

export function MyBox(props: MyBoxProps) {
  return <Flex as={props.as} size={props.size} />;
}

csantos-nydig avatar Nov 11 '21 15:11 csantos-nydig

type MyBoxProps = { // --- how do I get the as prop type here? --- // }

Based on what I can see in the code, this should work:

// Put this in your stitches.config.ts file
export type Polymorphic = string | React.ComponentType<any>

// In your components
type MyBoxProps = {
  as: Polymorphic,
}

Mind you, IIRC, React has a type for HTML element strings, but that may conflict with Stitches' types once you try to pass it in, I'm not sure. If anyone tries it out, please let us know how it went :)

LucasUnplugged avatar Dec 15 '21 22:12 LucasUnplugged

Hi @hadihallak - I'm still seeing Polymorphic as typescript errors when composing components even when using the latest 1.2.7-0 canary build (unless I'm misunderstanding how this should work with Stitches) - I've put together a code sandbox showing the issue here https://codesandbox.io/s/stitches-polymorphic-inheritance-typescript-error-d1xn1o?file=/src/Paragraph.component.tsx

ChrisDalley avatar Apr 06 '22 11:04 ChrisDalley

Please reopen. This issue persists.

hainessss avatar Apr 21 '22 20:04 hainessss

Hi,

This issue persits with me.

Configurations

  • @nrwl: 14.4.2
  • next: 12.1.6
  • react: 18.2.0
  • react-dom: 18.2.0
  • @stitches/react: 1.2.8

rntvicente avatar Aug 22 '22 13:08 rntvicente

If someone still stumbles across this - check whether you're using multiple levels of nesting. Because I have found that to be an issue: https://github.com/stitchesjs/stitches/issues/1105

s-h-a-d-o-w avatar Oct 07 '22 13:10 s-h-a-d-o-w