nit: improve types
This commit is contained in:
parent
dc21d085ec
commit
2022ce5b87
2 changed files with 28 additions and 18 deletions
|
@ -1,9 +1,9 @@
|
|||
---
|
||||
import { LocalImageProps, RemoteImageProps, getImage } from 'astro:assets';
|
||||
import { GetImageResult, ImageOutputFormat } from '../dist/@types/astro';
|
||||
import { getImage, type LocalImageProps, type RemoteImageProps } from 'astro:assets';
|
||||
import type { GetImageResult, ImageOutputFormat } from '../dist/@types/astro';
|
||||
import { isESMImportedImage } from '../dist/assets/internal';
|
||||
import { AstroError, AstroErrorData } from '../dist/core/errors/index.js';
|
||||
import { HTMLAttributes } from '../types';
|
||||
import type { HTMLAttributes } from '../types';
|
||||
|
||||
type Props = (LocalImageProps | RemoteImageProps) & {
|
||||
formats?: ImageOutputFormat[];
|
||||
|
@ -11,7 +11,7 @@ type Props = (LocalImageProps | RemoteImageProps) & {
|
|||
pictureAttributes?: HTMLAttributes<'picture'>;
|
||||
};
|
||||
|
||||
const {formats = ["webp"], pictureAttributes = {}, ...props} = Astro.props;
|
||||
const { formats = ['webp'], pictureAttributes = {}, ...props } = Astro.props;
|
||||
|
||||
if (props.alt === undefined || props.alt === null) {
|
||||
throw new AstroError(AstroErrorData.ImageMissingAlt);
|
||||
|
@ -47,7 +47,10 @@ if (fallbackImage.srcSet.values.length > 0) {
|
|||
<picture {...pictureAttributes}>
|
||||
{
|
||||
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} />
|
||||
|
|
|
@ -97,19 +97,26 @@ type ImageSharedProps<T> = T & {
|
|||
* ```
|
||||
*/
|
||||
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`.
|
||||
*/
|
||||
widths?: number[];
|
||||
/**
|
||||
* 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`)[];
|
||||
};
|
||||
} & (
|
||||
| {
|
||||
/**
|
||||
* 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`.
|
||||
*/
|
||||
widths?: number[];
|
||||
densities?: never;
|
||||
}
|
||||
| {
|
||||
/**
|
||||
* 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> & {
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue