sanity-codegen icon indicating copy to clipboard operation
sanity-codegen copied to clipboard

Could not find types for ____ Future versions of sanity-codegen will allow you to type them separately

Open ricokahler opened this issue 4 years ago • 4 comments

Currently if SanityCodegen finds a reference type that isn't used anywhere, it'll create an empty interface. A typical example of this happening is when using a sanity plugin that defines a new type such as the sanity code input plugin.

This solution works but I think there's room for improvement. Please leave suggestions!

ricokahler avatar Nov 30 '20 13:11 ricokahler

Maybe we could have a method to manually config or point to the the correct typings inside the sanity-codegent.config.ts?

eg the autocomplete-tags creates a tags currently the code gen returns

Could not find types for: "Tags". Ensure they are present in your schema. Future versions of sanity-codegen will allow you to type them separately.
[SanityCodeGen]: types written out to ~/studio/schema.d.ts)

// interface Tags = {}

so maybe instead we could define the tags to tell the codegen that the tags have been manually defined

import { SanityCodegenConfig } from 'sanity-codegen'

const config: SanityCodegenConfig = {
  schemaPath: './app/schema.ts',
  outputPath: './schema.d.ts',
  overrideTypes:{
    Tags:"./tags.d.ts" // unsure if there is better method of doing it here?
  }
}

paul-vd avatar Jan 13 '21 13:01 paul-vd

I'm able to work around this by defining these types in a sanity-custom.d.ts:

/// <reference types="@sanity-codegen/types" />

declare namespace Sanity {
  namespace Schema {
    interface Code extends Sanity.Document {
      _type: 'code';
      filename?: string;
      code?: string;
      syntax: string;
    }
  }
}

Then, in my generated-sanity.d.ts file, it knows what Sanity.Schema.Code is :)

good-idea avatar Aug 04 '21 19:08 good-idea

@good-idea Just wondering if you are still using this method as I've attempted to do the same today and am not having much luck so far. I'm using sanity-plugin-tags and trying to type the simple records it returns (an array of objects with keys of value and label).

My generated schema.d.ts file contains these document and field schemas (simplified for clarity):

export interface CaseStudy extends SanityDocument {
  _type: "caseStudy";

  /**
   * Technology tags — `technologyTags`
   *
   *
   */
  technologyTags?: TechnologyTags;
}

export type TechnologyTags = Tags;

/**
 * This interface is a stub. It was referenced in your sanity schema but
 * the definition was not actually found. Future versions of
 * sanity-codegen will let you type this explicity.
 */
type Tags = any;

In the same directory as the generated schema.d.ts file, I have added schema-custom.d.ts with the following contents based on your reply above:

/// <reference types="@sanity-codegen/types" />
declare namespace Sanity {
    namespace Schema {
        export type Tag = {
            label: string;
            value: string;
        }

        export type Tags = Tag[];
    }
}

Perhaps expecting this to "just work" is a bit optimistic but I'm new to Typescript (and VSCode, the editor I'm using) so unsure what else I need to do to make this work – in any case, as it stands, the technologyTags field of my caseStudy document still comes out as any.

Do I need to add some sort of import/reference from the generated file to the custom one, or vice-versa? Or am I causing problems declaring inside a namespace in the custom file, when the generated one doesn't (as far as I can tell) use a namespace?

@ricokahler If you have any inside info in this regard, it would be welcome!

Any tips on getting this over the line would be very much appreciated!

BigglesZX avatar Jul 29 '22 16:07 BigglesZX

Just realised that the solution above must be using the alpha branch since the stable branch doesn't use namespaces. At the moment I'm importing from the generated defs file and manually overriding with custom types, but still looking for a more elegant solution...

BigglesZX avatar Aug 27 '22 09:08 BigglesZX