0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-23 21:53:55 -05:00

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>
This commit is contained in:
Nate Moore 2022-10-26 10:14:08 -05:00 committed by GitHub
parent 0408376281
commit 0bf0758fb8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 0 deletions

View file

@ -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;
};
```

View file

@ -29,6 +29,7 @@
"default": "./astro.js" "default": "./astro.js"
}, },
"./env": "./env.d.ts", "./env": "./env.d.ts",
"./types": "./types.d.ts",
"./client": "./client.d.ts", "./client": "./client.d.ts",
"./client-base": "./client-base.d.ts", "./client-base": "./client-base.d.ts",
"./import-meta": "./import-meta.d.ts", "./import-meta": "./import-meta.d.ts",

11
packages/astro/types.d.ts vendored Normal file
View file

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