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

feat: uncomment polymorphic type #7069

Merged
merged 2 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .changeset/two-carrots-smile.md
Original file line number Diff line number Diff line change
@@ -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} />
```
9 changes: 6 additions & 3 deletions packages/astro/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']> }
>;