-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Change how IntrinsicElements type is defined #4915
Comments
I will defer to @Princesseuh who is our TS guru but what you are saying seems reasonable to me. |
I totally agree with this! I wonder if we could even make the Also notable: I have been pushing for having some built-in type utilities for patterns like this. In this case, my hope is that you can import an |
The suggestion makes total sense to me! This is a good workaround until we provide built-in type utilities like Nate suggest. If you're willing to, feel free to open a PR with this change. Otherwise, just let me know and I'll tackle it ASAP! Thank you for creating this issue! |
Hi, Is there any progress with this problem? It would be amazing be able to do this. Props should be the posible props of as, this is possible in React doing this: export interface Props {
as?: keyof astroHTML.JSX.DefinedIntrinsicElements;
class?: string;
}
const { class: className = "", as: Element = "a", ...props } = Astro.props; Thanks! |
If you want to allow the
looks a bit dodgy, but any attempts to abstract it led to failure on my end. It works well with react because you can type the component itself, and not just props. Astro 2.5 released some utilities for polymorphic components but they do not work for me, so I’m doing this instead. |
What version of
astro
are you using?1.2.5
Are you using an SSR adapter? If so, which one?
No
What package manager are you using?
npm
What operating system are you using?
Mac
Describe the Bug
I'm trying to implement an
as
prop to an Astro component. This prop should accept an HTML element tag name, and the component should render as that component. For example:To type the
as
prop, I'm trying to usekeyof astroHTML.JSX.IntrinsicElements
. Ideally, this would give me a union of all the possible HTML elements that Astro supports. However, the definition forIntrinsicElements
is all HTML elements, but it also includes a catchall at the endThis causes
typeof astroHTML.JSX.IntrinsicElements
to resolve to the typestring
, which isn't specific enough to allow theid
andclass
props on<Ele>
.Suggestion
Create a
DefinedIntrinsicElements
interface that only includes defined elements, and rewriteIntrinsicElements
to beThis way, my component can define its props with
and the passed prop will correctly be restricted to known HTML elements.
Link to Minimal Reproducible Example
(I couldn't figure out how to get StackBlitz to show red squiggle for type errors, but I believe this is set up correctly)
https://stackblitz.com/edit/github-mwmoww-zt2tqt?file=astro.config.mjs,src%2Fpages%2Findex.astro&on=stackblitz
Participation
The text was updated successfully, but these errors were encountered: