Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complex union type in Polymorphic components #11880

Open
1 task
walmartwarlord opened this issue Aug 29, 2024 · 5 comments
Open
1 task

Complex union type in Polymorphic components #11880

walmartwarlord opened this issue Aug 29, 2024 · 5 comments
Labels
feat: typescript Related to TypeScript (scope) needs response Issue needs response from OP needs triage Issue needs to be triaged

Comments

@walmartwarlord
Copy link

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

  • I am willing to submit a pull request for this issue.
@michaelpayne02
Copy link

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
Copy link
Author

walmartwarlord commented Aug 30, 2024

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
Copy link

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
Copy link
Contributor

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
Copy link
Member

@walmartwarlord does Armand's solution work for you?

@florian-lefebvre florian-lefebvre added needs response Issue needs response from OP feat: typescript Related to TypeScript (scope) labels Sep 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: typescript Related to TypeScript (scope) needs response Issue needs response from OP needs triage Issue needs to be triaged
Projects
None yet
Development

No branches or pull requests

4 participants