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

[v1][codegen] Reference fields are typed as any #298

Open
ochicf opened this issue May 25, 2023 · 0 comments
Open

[v1][codegen] Reference fields are typed as any #298

ochicf opened this issue May 25, 2023 · 0 comments

Comments

@ochicf
Copy link
Contributor

ochicf commented May 25, 2023

Generated types for reference fields result in that field being any. This is caused because the Sanity.Reference<...> type is used, which is any.

Example:

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

namespace Sanity.MyNamespace.Schema {
  type MyType =
  | {
      _id: string;
      _type: "myType";
      myReferenceField?: Sanity.Reference<Sanity.Ref.Ref_1234>;
                                // ^? type Sanity.Reference = /*unresolved*/ any 

    }
    | undefined;
}

Manually adding the following code at the top of the generated types fixes the issue, so I guess this should be generated aswell by the codegen:

namespace Sanity {
  type Reference<T> = T | {
    _type: string
    _ref: string
    _key?: string
    _weak?: boolean
    _strengthenOnPublish?: {
      type: string
      weak?: boolean
      template?: {
        id: string
        params: Record<string, string | number | boolean>
      }
    }
  };
}

Note that I copied the object reference definition from @sanity/types, so maybe it could be used from there?


EDIT: I just realised all those types are already defined in @sanity-codegen/types and I didn't have it installed where I'm using the generated schemas (I'm in a monorepo with project+studio). After installing it's still not picking them up, will continue investigating.

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

1 participant