Skip to content

Commit c1669c0

Browse files
authored
feat: uncomment polymorphic type (#7069)
1 parent dd8dd6b commit c1669c0

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

.changeset/two-carrots-smile.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
'astro': minor
3+
---
4+
5+
Added `Polymorphic` type helper to `astro/types` to easily create polymorphic components:
6+
7+
```astro
8+
---
9+
import { HTMLTag, Polymorphic } from 'astro/types';
10+
11+
type Props<Tag extends HTMLTag> = Polymorphic<{ as: Tag }>;
12+
13+
const { as: Tag, ...props } = Astro.props;
14+
---
15+
16+
<Tag {...props} />
17+
```

packages/astro/types.d.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ export type HTMLAttributes<Tag extends HTMLTag> = Omit<
99
keyof Omit<AstroBuiltinAttributes, 'class:list'>
1010
>;
1111

12-
// TODO: Enable generic/polymorphic types once compiler output stabilizes in the Language Server
13-
// type PolymorphicAttributes<P extends { as: HTMLTag }> = Omit<(P & HTMLAttributes<P['as']>), 'as'> & { as?: P['as'] };
14-
// export type Polymorphic<P extends { as: HTMLTag }> = PolymorphicAttributes<Omit<P, 'as'> & { as: NonNullable<P['as']>}>;
12+
type PolymorphicAttributes<P extends { as: HTMLTag }> = Omit<P & HTMLAttributes<P['as']>, 'as'> & {
13+
as?: P['as'];
14+
};
15+
export type Polymorphic<P extends { as: HTMLTag }> = PolymorphicAttributes<
16+
Omit<P, 'as'> & { as: NonNullable<P['as']> }
17+
>;

0 commit comments

Comments
 (0)