diff options
author | 2022-10-26 10:14:08 -0500 | |
---|---|---|
committer | 2022-10-26 10:14:08 -0500 | |
commit | 0bf0758fb831a91f6628e10a5baabf02f30f1f41 (patch) | |
tree | 1faff650389ef914c9ea5c18da3e4315bffba479 | |
parent | 04083762810a1a9e078a7e68edab945c8063b1ab (diff) | |
download | astro-0bf0758fb831a91f6628e10a5baabf02f30f1f41.tar.gz astro-0bf0758fb831a91f6628e10a5baabf02f30f1f41.tar.zst astro-0bf0758fb831a91f6628e10a5baabf02f30f1f41.zip |
Add `astro/types` for common prop patterns (#5147)
* 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 <nate@astro.build>
-rw-r--r-- | .changeset/dirty-cycles-lick.md | 16 | ||||
-rw-r--r-- | packages/astro/package.json | 1 | ||||
-rw-r--r-- | packages/astro/types.d.ts | 11 |
3 files changed, 28 insertions, 0 deletions
diff --git a/.changeset/dirty-cycles-lick.md b/.changeset/dirty-cycles-lick.md new file mode 100644 index 000000000..e8e39be9b --- /dev/null +++ b/.changeset/dirty-cycles-lick.md @@ -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; +}; +``` diff --git a/packages/astro/package.json b/packages/astro/package.json index 527a87e1a..331ef6b27 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -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", diff --git a/packages/astro/types.d.ts b/packages/astro/types.d.ts new file mode 100644 index 000000000..ae56cd2dc --- /dev/null +++ b/packages/astro/types.d.ts @@ -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']>}>; |