diff --git a/.changeset/two-carrots-smile.md b/.changeset/two-carrots-smile.md new file mode 100644 index 000000000000..d5b046b0bdb3 --- /dev/null +++ b/.changeset/two-carrots-smile.md @@ -0,0 +1,17 @@ +--- +'astro': minor +--- + +Added `Polymorphic` type helper to `astro/types` to easily create polymorphic components: + +```astro +--- +import { HTMLTag, Polymorphic } from 'astro/types'; + +type Props = Polymorphic<{ as: Tag }>; + +const { as: Tag, ...props } = Astro.props; +--- + + +``` diff --git a/packages/astro/types.d.ts b/packages/astro/types.d.ts index a8ad0267b201..0939c828aac5 100644 --- a/packages/astro/types.d.ts +++ b/packages/astro/types.d.ts @@ -9,6 +9,9 @@ export type HTMLAttributes = Omit< keyof Omit >; -// TODO: Enable generic/polymorphic types once compiler output stabilizes in the Language Server -// type PolymorphicAttributes

= Omit<(P & HTMLAttributes), 'as'> & { as?: P['as'] }; -// export type Polymorphic

= PolymorphicAttributes & { as: NonNullable}>; +type PolymorphicAttributes

= Omit

, 'as'> & { + as?: P['as']; +}; +export type Polymorphic

= PolymorphicAttributes< + Omit & { as: NonNullable } +>;