I recently started working on building a website using NEXT.js with typescript. I have limited experience with typescript and currently facing an issue related to ImageLoader as cloudflare pages don't support the default image loader. However, I am unable to figure out why my code is not functioning properly.
Here is the snippet of my Loader code:
const normalizeSrc = (src: string) => {
return src.startsWith("/") ? src.slice(1) : src;
};
const cloudflareLoader = ({
src,
width,
quality,
}: {
src: string;
width: number;
quality: number;
}) => {
const params = [`width=${width}`];
if (quality) {
params.push(`quality=${quality}`);
}
const paramsString = params.join(".");
return `/cdn-cgi/images/${paramsString}/${normalizeSrc(src)}`;
};
And here is how I'm implementing the Image:
<Image
loader={cloudflareLoader}
src="/images/image-name.png" // the image is located in the public/images folder
height={120}
width={120}
alt=""
/>
The error message that I'm encountering is:
Type '({ src, width, quality, }: { src: string; width: number; quality: number; }) => string' is not assignable to type 'ImageLoader'.
Types of parameters '__0' and 'p' are incompatible.
Type 'ImageLoaderProps' is not assignable to type '{ src: string; width: number; quality: number; }'.
Types of property 'quality' are incompatible.
Type 'number | undefined' is not assignable to type 'number'.
Type 'undefined' is not assignable to type 'number'.
Any suggestions or insights on what steps should I take to resolve this issue?
I found some helpful information at