question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

To repro:

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

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

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

13reactions
LucasUnpluggedcommented, Dec 15, 2021

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 😃

6reactions
ChrisDalleycommented, Apr 6, 2022

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript error when sharing styles across components that ...
I am creating a variable called sharedTypo using the css function, to share styles across components. It has variant called variant with a...
Read more >
Typescript styled-component error: "Type '{ children: string
I found a great solution that has nothing to do with typescript or styled-components. It is enough to create a node through React.createElement....
Read more >
Composing React Components with TypeScript - Pluralsight
Introduction. In this guide, you will learn how to compose React.js components using the compile-to-JavaScript language, TypeScript.
Read more >
TypeScript errors and how to fix them
error TS1337: An index signature parameter type cannot be a literal type or generic type. Consider using a mapped object type instead. Broken...
Read more >
Build strongly typed polymorphic components with React and ...
If you go ahead and use this component wrongly, e.g., passing a valid as prop with other incompatible props, you'll get an error....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found