Can I know the what is the type declaration for images that are imported from public folder in nextjs.
I created a file called data in utils folder. Inside data file I imported the images from public folder and created a array of objects with keys like tile, desc, image.
For eg
const users = [ { avatar: img1, name: "some name", age: 12, }, { avatar: img2, name: "some name", age: 14, } ]
when i gave type for users obj it is showing an error of
Types of property 'img' are incompatible. Type 'StaticImageData' is missing the following properties from type 'HTMLImageElement': align, alt, border, complete, and 306 more.ts(2322)
Can some one tell me the exact type for images imported from public folder and used it in array of objects
I tried to change the staticimagetype but it is unable to find
2
Answers
To use the Next.js next/image component to display an image from the public folder, you can simply provide the image file path as the src prop of the next/image component.
Here’s an example:
In your case:
if you want to use static path for images from public folder
And if you are going to use the image from object by importing it
When you import an image from a public folder, those images are parsed by the compiler as an object of type
StaticImageData
with all the necessary details of that image. They are not imported as components or HTML elements. This is why when you try to assign them toavatar
property which has a type ofHTMLImageElement
, typescript gives you are type mismatch error. You can avoid this by using imported image data as src forImage
component:You can learn more about Next.js static file serving here and Next Image here.