Skip to content

How to properly type Box when using polymorphism via as? #1978

Answered by hasparus
rafaelrinaldi asked this question in Q&A
Discussion options

You must be logged in to vote

Please don't. theme-ui and @theme-ui/components export Input component.

I'd discourage from using polymorphic as prop in TypeScript if you care about your build times. It's a footgun. I managed to crash TypeScript compiler with it a couple times.

Radix UI has a reusable library for proper as prop typings if you really need it.

https://www.radix-ui.com/docs/primitives/utilities/polymorphic

It's probably the best executed as prop implementation you can get, and they still deprecated it in favor of child cloning asChild prop.

If you want to read more about it: Andrew Branch from TypeScript team has a great blogpost about polymorphic components -- https://blog.andrewbran.ch/polymorphic-react-…

Replies: 3 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by lachlanjc
Comment options

You must be logged in to vote
1 reply
@hasparus
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants