Skip to content

Commit

Permalink
Add astro/types for common prop patterns (withastro#5147)
Browse files Browse the repository at this point in the history
* wip: add type-utils

* feat: update type-utils

* feat: RequireDefaultSlot => WithChildren

* chore: add changeset

* chore: move types to ./types

* chore: update changeset

* Update dirty-cycles-lick.md

Co-authored-by: Nate Moore <[email protected]>
  • Loading branch information
natemoo-re and natemoo-re committed Oct 26, 2022
1 parent 0408376 commit 0bf0758
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .changeset/dirty-cycles-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
'astro': minor
---

Add `astro/types` entrypoint. These utilities can be used for common prop type patterns.

## `HTMLAttributes`

If you would like to extend valid HTML attributes for a given HTML element, you may use the provided `HTMLAttributes` type—it accepts an element name and returns the valid HTML attributes for that element name.

```ts
import { HTMLAttributes } from 'astro/types';
interface Props extends HTMLAttributes<'a'> {
myProp?: string;
};
```
1 change: 1 addition & 0 deletions packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"default": "./astro.js"
},
"./env": "./env.d.ts",
"./types": "./types.d.ts",
"./client": "./client.d.ts",
"./client-base": "./client-base.d.ts",
"./import-meta": "./import-meta.d.ts",
Expand Down
11 changes: 11 additions & 0 deletions packages/astro/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import './astro-jsx';
import { AstroBuiltinAttributes } from './dist/@types/astro';

/** Any supported HTML or SVG element name, as defined by the HTML specification */
export type HTMLTag = keyof astroHTML.JSX.DefinedIntrinsicElements;
/** The built-in attributes for any known HTML or SVG element name */
export type HTMLAttributes<Tag extends HTMLTag> = Omit<astroHTML.JSX.IntrinsicElements[Tag], keyof AstroBuiltinAttributes>;

// 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']>}>;

0 comments on commit 0bf0758

Please sign in to comment.