Skip to content
This repository has been archived by the owner on Mar 26, 2024. It is now read-only.

TS2503: Cannot find namespace 'Sanity' #314

Closed
goodwin64 opened this issue Jul 10, 2023 · 3 comments
Closed

TS2503: Cannot find namespace 'Sanity' #314

goodwin64 opened this issue Jul 10, 2023 · 3 comments

Comments

@goodwin64
Copy link

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
Copy link
Contributor

ochicf commented Jul 19, 2023

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:

  • @sanity-codegen/types is installed as dev dependency (or at least present by some other dependency)
  • the file with the generated types is included by your tsconfig.json

Hope that helps!

(full disclaimer: I also have problems with certain typings, as stated here #298)

@goodwin64
Copy link
Author

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
Copy link
Author

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants