This is my first official post on Stack so bear with me; I am currently using Nextjs with Sanity as my headless CMS and I'm encountering what seems to be a TypeScript type issue. GH: https://github.com/GabrielPedroza/e-commerce/blob/main/components/Product.tsx
<Image src={urlFor(image && image[0]!)} />
import imageUrlBuilder from "@sanity/image-url"
import { SanityImageSource } from "@sanity/image-url/lib/types/types"
export const client = sanityClient({
projectId: "gz95qrem",
dataset: "production",
apiVersion: "2022-04-27",
useCdn: true,
token: process.env.NEXT_PUBLIC_SANITY_TOKEN, // for security purposes
})
const builder = imageUrlBuilder(client)
export const urlFor = (source: SanityImageSource) => {
return builder.image(source)
}
Type 'ImageUrlBuilder' is not assignable to type 'string | StaticImport'.
Property 'src' is missing in type 'ImageUrlBuilder' but required in type 'StaticImageData'.ts(2322)
image.d.ts(19, 5): 'src' is declared here.
image.d.ts(29, 5): The expected type comes from property 'src' which is declared here on type 'IntrinsicAttributes & Omit<DetailedHTMLProps<ImgHTMLAttributes<HTMLImageElement>, HTMLImageElement>, "ref" | ... 4 more ... | "loading"> & { ...; }'
Server Error
Error: Image is missing required "src" property. Make sure you pass "src" in props to the `next/image` component. Received: {}
Call Stack
Image
node_modules/next/dist/client/image.js (160:18)
renderWithHooks
file:///Users/gabrielpedroza/Code/e-commerce/node_modules/react-dom/cjs/react-dom-server.browser.development.js (5448:16)
renderIndeterminateComponent
file:///Users/gabrielpedroza/Code/e-commerce/node_modules/react-dom/cjs/react-dom-server.browser.development.js (5521:15)
renderElement
file:///Users/gabrielpedroza/Code/e-commerce/node_modules/react-dom/cjs/react-dom-server.browser.development.js (5736:7)
renderNodeDestructive
file:///Users/gabrielpedroza/Code/e-commerce/node_modules/react-dom/cjs/react-dom-server.browser.development.js (5875:11)
renderNode
file:///Users/gabrielpedroza/Code/e-commerce/node_modules/react-dom/cjs/react-dom-server.browser.development.js (6009:12)
renderHostElement
file:///Users/gabrielpedroza/Code/e-commerce/node_modules/react-dom/cjs/react-dom-server.browser.development.js (5433:3)
renderElement
file:///Users/gabrielpedroza/Code/e-commerce/node_modules/react-dom/cjs/react-dom-server.browser.development.js (5742:5)
renderNodeDestructive
file:///Users/gabrielpedroza/Code/e-commerce/node_modules/react-dom/cjs/react-dom-server.browser.development.js (5875:11)
renderNode
file:///Users/gabrielpedroza/Code/e-commerce/node_modules/react-dom/cjs/react-dom-server.browser.development.js (6009:12)
One way to remove the error is to just use a string or use ts-ignore but this obviously can't be used in my scenario since I'm using Sanity. My temporary dirty solution is to use the regular img tag and use a ts-ignore so it would work. I tried asking at the official Next discord server, overall googling and reading docs but can't really find a solution to this.
url()
method to return an actual string, i.e.urlFor(image).width(200).url()
? See sanity.io/docs/image-url#usage.