withastro / astro

The web framework for content-driven websites. ⭐️ Star to support our work!
https://astro.build
Other
45.54k stars 2.39k forks source link

Complex union type in Polymorphic components #11880

Open walmartwarlord opened 2 weeks ago

walmartwarlord commented 2 weeks ago

Astro Info

Astro                    v4.15.1
Node                     v20.17.0
System                   macOS (arm64)
Package Manager          npm
Output                   static
Adapter                  none
Integrations             none

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

Getting a TS error in a fresh project with a simple Polymorphic component:

Screenshot 2024-08-29 at 3 30 42 PM
ts: Expression produces a union type that is too complex to represent.

What's the expected result?

It should not have any TypeScript error as it does have a default value.

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-e3ih6w?file=src%2Fpages%2Findex.astro

Participation

michaelpayne02 commented 2 weeks ago

I have this error as well. The prop spreading is what inflates the number of type variations that TS needs to evaluate and check.

walmartwarlord commented 2 weeks ago

I have this error as well. The prop spreading is what inflates the number of type variations that TS needs to evaluate and check.

So if you dont spread it, the TS error is gone? If yes, do you have a basic example of a component

michaelpayne02 commented 2 weeks ago

I believe so, yes. I encountered this a while ago and the only way I was able to get the error to go away was by removing the prop spreading when destructuring Astro.props. I think there are so many different HTMLTag type variations that when combined with the spread operator, the number of type permutations grows exponentially.

ArmandPhilippot commented 1 week ago

In CustomButton.astro, you need to define the default value in the Props generic parameter as well:

-type Props<Tag extends HTMLTag> = Polymorphic<{ as: Tag }>;
+type Props<Tag extends HTMLTag = "button"> = Polymorphic<{ as: Tag }>;

I think this is why Typescript complains about too complex union type. By doing this you will see that props is no longer inferred as any and your index.astro page no longer reports an error.

florian-lefebvre commented 1 day ago

@walmartwarlord does Armand's solution work for you?