feat: uncomment polymorphic type (#7069)

This commit is contained in:
Erika 2023-05-17 15:38:27 +02:00 committed by GitHub
parent dd8dd6b31e
commit c1669c0011
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 3 deletions

View file

@ -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<Tag extends HTMLTag> = Polymorphic<{ as: Tag }>;
const { as: Tag, ...props } = Astro.props;
---
<Tag {...props} />
```

View file

@ -9,6 +9,9 @@ export type HTMLAttributes<Tag extends HTMLTag> = Omit<
keyof Omit<AstroBuiltinAttributes, 'class:list'>
>;
// TODO: Enable generic/polymorphic types once compiler output stabilizes in the Language Server
// type PolymorphicAttributes<P extends { as: HTMLTag }> = Omit<(P & HTMLAttributes<P['as']>), 'as'> & { as?: P['as'] };
// export type Polymorphic<P extends { as: HTMLTag }> = PolymorphicAttributes<Omit<P, 'as'> & { as: NonNullable<P['as']>}>;
type PolymorphicAttributes<P extends { as: HTMLTag }> = Omit<P & HTMLAttributes<P['as']>, 'as'> & {
as?: P['as'];
};
export type Polymorphic<P extends { as: HTMLTag }> = PolymorphicAttributes<
Omit<P, 'as'> & { as: NonNullable<P['as']> }
>;