ari*_*ain 10 typescript next.js nextjs-image
我正在使用 react dropzone 在我的简单应用程序中上传多图像。为了显示哪种类型的图像被删除,我使用 TypeScript 制作了一个单独的组件。但是 Next.js 图像 src 显示错误,如类型:
'{ src: string; alt: string; }' is not assignable to type 'IntrinsicAttributes & ImageProps'.
Type '{ src: string; alt: string; }' is not assignable to type 'ObjectImageProps'.
Types of property 'src' are incompatible.
Type 'string' is not assignable to type 'StaticImport'.
Run Code Online (Sandbox Code Playgroud)
渲染文件.ts:
'{ src: string; alt: string; }' is not assignable to type 'IntrinsicAttributes & ImageProps'.
Type '{ src: string; alt: string; }' is not assignable to type 'ObjectImageProps'.
Types of property 'src' are incompatible.
Type 'string' is not assignable to type 'StaticImport'.
Run Code Online (Sandbox Code Playgroud)
类型.ts:
import { IFile } from "../../libs/types";
import { sizeInMb } from "../../libs/sizeInMb";
import { FunctionComponent } from "react";
import Image from "next/image"
const RenderFile: FunctionComponent<{
file: IFile;
}> = ({ file: { formate, sizeInBytes, name } }) => {
return (
<div>
<Image src={`/images/${formate}.png`} alt="image"/>
<span>{name}</span>
<span>{sizeInMb(sizeInBytes)}</span>
</div>
);
};
export default RenderFile;
Run Code Online (Sandbox Code Playgroud)
我在 src props 中的错误是什么?
ale*_*xor 10
问题是next/image'sImage期望相当复杂的类型,ImageProps因为它是道具:
type StringImageProps = {
src: string
} & (
| { width?: never; height?: never; layout: 'fill' }
| {
width: number | string
height: number | string
layout?: Exclude<LayoutValue, 'fill'>
}
) &
(
| {
placeholder?: Exclude<PlaceholderValue, 'blur'>
blurDataURL?: never
}
| { placeholder: 'blur'; blurDataURL: string }
)
type ObjectImageProps = {
src: StaticImport
width?: number | string
height?: number | string
layout?: LayoutValue
placeholder?: PlaceholderValue
blurDataURL?: never
}
export type ImageProps = Omit<
JSX.IntrinsicElements['img'],
'src' | 'srcSet' | 'ref' | 'width' | 'height' | 'loading' | 'style'
> & {
loader?: ImageLoader
quality?: number | string
priority?: boolean
loading?: LoadingValue
unoptimized?: boolean
objectFit?: ImgElementStyle['objectFit']
objectPosition?: ImgElementStyle['objectPosition']
} & (StringImageProps | ObjectImageProps)
Run Code Online (Sandbox Code Playgroud)
由于您不是从本地导入导入图像,因此您剩下的唯一结构是StringImageProps. 要符合它,您必须提供以下道具集之一:
<Image src={string} layout="fill" />
// or
<Image src={string} width={number} height={number} /> // with optional `layout` prop but not of type 'fill'
Run Code Online (Sandbox Code Playgroud)
两种变体都可以使用 optional placeholder(不是“blur”类型)或必需placeholder: 'blur' 和 进行扩展blurDataURL: string。
只有在此之后,您才能将 nativeimage的属性提供为alt.
导入类型ImageLoaderProps为我解决了这个问题。
例子:
import Image from 'next/image';
import type { ImageLoaderProps } from 'next/image';
const myLoader = ({ src, width, quality }: ImageLoaderProps) => {
return `https://example.com/?${src}?w=${width}&q=${quality}`;
};
export default function TestComponent(props: Record<string, string>) {
const { imageResource } = props;
return (
<Image
loader={myLoader}
src={`/${imageResource}`}
width="20%"
height="20%"
/>
);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2204 次 |
| 最近记录: |