Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type error on array of block and image #712

Open
LeoLYW12138 opened this issue Apr 11, 2024 · 4 comments
Open

Type error on array of block and image #712

LeoLYW12138 opened this issue Apr 11, 2024 · 4 comments

Comments

@LeoLYW12138
Copy link

I'm trying to transform sanity schema to sanity-typed schema when I encounter this error.

The official doc suggests that we can add images to Portable Text by appending a new type object to the array as such

export default {
  name: 'content',
  type: 'array',
  title: 'Content',
  of: [
    {
      type: 'block'
    },
    {
      type: 'image'
    }
  ]
}

However, when I define equivalent schema with sanity-typed, like this

import { defineArrayMember, defineType } from "@sanity-typed/types";

const content = defineType({
  name: "content",
  type: "array",
  of: [
    defineArrayMember({
      type: "block",
    }),
    defineArrayMember({
      type: "image",
    }),
  ],
});

export default content;

I am having type error

Type error: Type '[ArrayMemberDefinition<"block", string, IntrinsicTypeName, StrictDefinition, number, string, string, false, BlockStyleDefault, BlockListItemDefault, BlockMarkDecoratorDefault, never, false, never, never, false>, ArrayMemberDefinition<...>]' is not assignable to type '[ArrayMemberDefinition<"block", string, IntrinsicTypeName, StrictDefinition, number, string, string, false, BlockStyleDefault, BlockListItemDefault, BlockMarkDecoratorDefault, never, false, never, never, false>, ...ArrayMemberDefinition<...>[]]'.
  Type at position 1 in source is not compatible with type at position 1 in target.
    Type 'ArrayMemberDefinition<"image", string, IntrinsicTypeName, StrictDefinition, number, string, string, false, BlockStyleDefault, BlockListItemDefault, BlockMarkDecoratorDefault, never, false, never, never, false>' is not assignable to type 'ArrayMemberDefinition<"block", string, IntrinsicTypeName, StrictDefinition, number, string, string, false, BlockStyleDefault, BlockListItemDefault, BlockMarkDecoratorDefault, never, false, never, never, false>'.
      Type 'ArrayMemberDefinition<"image", string, IntrinsicTypeName, StrictDefinition, number, string, string, false, BlockStyleDefault, BlockListItemDefault, BlockMarkDecoratorDefault, never, false, never, never, false>' is not assignable to type 'Omit<{ type: "block"; initialValue?: InitialValueProperty<any, any[]>; options?: BlockOptions | undefined; components?: { block?: ComponentType<BlockProps> | undefined; } | undefined; ... 12 more ...; validation?: ValidationBuilder<...> | undefined; }, "name">'.
        Types of property 'type' are incompatible.
          Type '"image"' is not assignable to type '"block"'.

  4 |   name: "content",
  5 |   type: "array",
> 6 |   of: [
    |   ^
  7 |     defineArrayMember({
  8 |       type: "block",
  9 |     }),

It seems that I cannot put array members of different types into the same array under the current version of @sanity-typed/types@6.3.3 and typescript@5.4.5

@gercordero
Copy link

gercordero commented Apr 29, 2024

I'm having the exact same issue. The Portable Text component works fine in Sanity studio but typescript complains when having more than 1 type of array member inside the of array.

@MartCube
Copy link

I'm having the exact same issue. The Portable Text component works fine in Sanity studio but typescript complains when having more than 1 type of array member inside the of array.

Im facing the same error

@saiichihashimoto
Copy link
Owner

I've tried this locally with no issues. What version of typescript, sanity, and @sanity-typed/* are you using? @LeoLYW12138 @gercordero @MartCube

@LeoLYW12138
Copy link
Author

@saiichihashimoto

"@sanity-typed/next-sanity": "^2.1.2",
"@sanity-typed/types": "^6.3.3",
"sanity": "^3.37.2",
"typescript": "5.4.5"

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

No branches or pull requests

4 participants