ricokahler / sanity-codegen

Generate TypeScript types from your Sanity.io schemas
sanity-codegen-dev.vercel.app
MIT License
270 stars 19 forks source link

TS2503: Cannot find namespace 'Sanity' #314

Closed goodwin64 closed 1 year ago

goodwin64 commented 1 year ago

Using the same approach as in README:

interface Props {
  book: Sanity.Default.Schema.Book; // no import needed. just use it
}

I do the same in my code but it doesn't give me proper types:

const url = `https://${this.projectId}.api.sanity.io/v2021-10-21/data/query/${this.dataset}?query=${QUERY}`;

// fetch the content
const result = await fetch(url)
  .then((res) => res.json())
  .then(({ result }: { result: Sanity.Default.Schema.Adventure[] }) => {
    const firstAdventure = result[0];
    console.log(firstAdventure.adventureCode); // Intellisense doesn't show the type
    console.log(firstAdventure.contactNumberDestination); // nor here
    return result;
  });

return result;
}

moreover, ESlint complains to the Sanity namespace being not found: image

Are there any TS config changes needed to be done additionally to recognise the Sanity namespace?

ochicf commented 1 year ago

Hi @goodwin64!

The Sanity namespace is added by @sanity-codegen/types, and then augmented by the types generated by the codegen (adding your namespace and your types).

So I would say that you need to make sure that:

Hope that helps!

(full disclaimer: I also have problems with certain typings, as stated here https://github.com/ricokahler/sanity-codegen/issues/298)

goodwin64 commented 1 year ago

Heyyy @ochicf! Thanks for the reply!

We've made it a slightly different way eventually - moved our types to GraphQL schema so we just need to update it there (needed by arch req-s).

But yeah, compared to how easy it was to setup a Sanity client and fill in the content, the type-gen process feels a bit raw.

goodwin64 commented 1 year ago

Closing as not relevant but feel free to reopen later/link other issues here.