nit: improve types

This commit is contained in:
Princesseuh 2023-10-06 18:58:07 +02:00
parent dc21d085ec
commit 2022ce5b87
No known key found for this signature in database
GPG key ID: 105BBD6D57F2B0C0
2 changed files with 28 additions and 18 deletions

View file

@ -1,9 +1,9 @@
--- ---
import { LocalImageProps, RemoteImageProps, getImage } from 'astro:assets'; import { getImage, type LocalImageProps, type RemoteImageProps } from 'astro:assets';
import { GetImageResult, ImageOutputFormat } from '../dist/@types/astro'; import type { GetImageResult, ImageOutputFormat } from '../dist/@types/astro';
import { isESMImportedImage } from '../dist/assets/internal'; import { isESMImportedImage } from '../dist/assets/internal';
import { AstroError, AstroErrorData } from '../dist/core/errors/index.js'; import { AstroError, AstroErrorData } from '../dist/core/errors/index.js';
import { HTMLAttributes } from '../types'; import type { HTMLAttributes } from '../types';
type Props = (LocalImageProps | RemoteImageProps) & { type Props = (LocalImageProps | RemoteImageProps) & {
formats?: ImageOutputFormat[]; formats?: ImageOutputFormat[];
@ -11,7 +11,7 @@ type Props = (LocalImageProps | RemoteImageProps) & {
pictureAttributes?: HTMLAttributes<'picture'>; pictureAttributes?: HTMLAttributes<'picture'>;
}; };
const {formats = ["webp"], pictureAttributes = {}, ...props} = Astro.props; const { formats = ['webp'], pictureAttributes = {}, ...props } = Astro.props;
if (props.alt === undefined || props.alt === null) { if (props.alt === undefined || props.alt === null) {
throw new AstroError(AstroErrorData.ImageMissingAlt); throw new AstroError(AstroErrorData.ImageMissingAlt);
@ -47,7 +47,10 @@ if (fallbackImage.srcSet.values.length > 0) {
<picture {...pictureAttributes}> <picture {...pictureAttributes}>
{ {
Object.entries(optimizedImages).map(([_, image]) => ( Object.entries(optimizedImages).map(([_, image]) => (
<source srcset={`${image.src}, ` + image.srcSet.attribute} type={image.srcSet.values[0].attributes?.type} /> <source
srcset={`${image.src}, ` + image.srcSet.attribute}
type={image.srcSet.values[0].attributes?.type}
/>
)) ))
} }
<img src={fallbackImage.src} {...additionalAttributes} {...fallbackImage.attributes} /> <img src={fallbackImage.src} {...additionalAttributes} {...fallbackImage.attributes} />

View file

@ -97,19 +97,26 @@ type ImageSharedProps<T> = T & {
* ``` * ```
*/ */
height?: number | `${number}`; height?: number | `${number}`;
/** } & (
* A list of widths to generate images for. The value of this property will be used to assign the `srcset` property on the final `img` element. | {
* /**
* This attribute is incompatible with `densities`. * A list of widths to generate images for. The value of this property will be used to assign the `srcset` property on the final `img` element.
*/ *
widths?: number[]; * This attribute is incompatible with `densities`.
/** */
* A list of densities to generate images for. The value of this property will be used to assign the `srcset` property on the final `img` element. widths?: number[];
* densities?: never;
* This attribute is incompatible with `widths`. }
*/ | {
densities?: (number | `${number}x`)[]; /**
}; * A list of densities to generate images for. The value of this property will be used to assign the `srcset` property on the final `img` element.
*
* This attribute is incompatible with `widths`.
*/
densities?: (number | `${number}x`)[];
widths?: never;
}
);
export type LocalImageProps<T> = ImageSharedProps<T> & { export type LocalImageProps<T> = ImageSharedProps<T> & {
/** /**