sanity icon indicating copy to clipboard operation
sanity copied to clipboard

Wrong type when using image-url like in the example?

Open silvio-e opened this issue 2 years ago • 2 comments
trafficstars

Following example from here:

import imageUrlBuilder from '@sanity/image-url'

export default defineNuxtPlugin(() => {
  const builder = imageUrlBuilder(useSanity().config) // <-- here is the problem
  function urlFor(source) {
    return builder.image(source).auto('format')
  }
  return {
    provide: { urlFor }
  }
})

Doesn't work for me as the type seems wrong:

Argument of type 'SanityConfiguration' is not assignable to parameter of type 'SanityClientLike | SanityProjectDetails | SanityModernClientLike | undefined'.
  Type 'SanityConfiguration' is not assignable to type 'SanityProjectDetails'.
    Types of property 'dataset' are incompatible.
      Type 'string | undefined' is not assignable to type 'string'.
        Type 'undefined' is not assignable to type 'string'.ts(2345)

Should this work?

silvio-e avatar Oct 05 '23 15:10 silvio-e

I've got the same issue, what is the correct type for useSanity().config ?

notflip avatar Jul 15 '24 14:07 notflip

Also running into this issue. My current workaround to just suppress the typescript error:

// @ts-ignore
const builder = imageUrlBuilder(useSanity().config);

It looks like ImageUrlBuilder expects one of these types:

SanityClientLike | SanityProjectDetails | SanityModernClientLike

But useSanity().config is of type ClientConfig.

Wondering if this console warning might be related since this is the line in my file where builder is defined:

sanity.ts:7 [nuxt] Could not access `sanity`. The only available runtime config keys on the client side are `public` and `app`. See https://nuxt.com/docs/guide/going-further/runtime-config for more information.

davidstackio avatar Jul 25 '24 02:07 davidstackio