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

Type error on array of block and image

Open LeoLYW12138 opened this issue 10 months ago • 3 comments

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/[email protected] and [email protected]

LeoLYW12138 avatar Apr 11 '24 16:04 LeoLYW12138

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.

gercordero avatar Apr 29 '24 01:04 gercordero

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

MartCube avatar Apr 29 '24 10:04 MartCube

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

saiichihashimoto avatar May 13 '24 21:05 saiichihashimoto

@saiichihashimoto

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

LeoLYW12138 avatar May 17 '24 10:05 LeoLYW12138

@saiichihashimoto any updates or tips on this? New to your tool but running into this immediately.

All relevant package versions:

"@sanity-typed/groq": "^1.8.14",
"@sanity-typed/next-sanity": "^2.1.4",
"@sanity-typed/types": "^6.3.5",
"@sanity/client": "6.21.0",
"@sanity/icons": "3.3.0",
"@sanity/image-url": "1.0.2",
"@sanity/preview-url-secret": "1.6.18",
"@sanity/react-loader": "1.10.4",
"@sanity/ui": "2.6.8",
"@sanity/vision": "3.50.0",
"@sanity/visual-editing": "2.1.6",
"sanity": "3.50.0",

Commit on the repo where this is added: https://github.com/panzacoder/gruntwork-sanity/pull/1/commits/1b5f074d4f3321830d1a742c0747a18096a63400

panzacoder avatar Jul 15 '24 20:07 panzacoder

It looks like the issue is on this line.

The type of the of property is TupleOfLength<TMemberDefinition, 1>, so the first element of the array was more strongly typed than the rest of the elements. This caused any further elements having different type properties to be in conflict. The error goes away and all tests still pass when the type is changed to TMemberDefinition[]. I don't have very deep knowledge about the rest of the codebase so I'm unsure if this change is ultimately correct since it would change the type from a tuple to an array, but, nevertheless, the issue does seem to stem from this point.

sseasie avatar Jul 17 '24 15:07 sseasie

Honestly, these tuples have been more trouble than they're worth. 😔

@sseasie can you make a PR with that change? Make the commit message be a fix and I'll take a look then.

saiichihashimoto avatar Jul 17 '24 22:07 saiichihashimoto

Fix has gone in!

Btw, these packages have a peerDependency of "typescript": "5.1.3 || 5.1.5 || 5.1.6 || 5.2.2" so, currently, newer versions of typescript can break. @LeoLYW12138 @sseasie

saiichihashimoto avatar Jul 18 '24 14:07 saiichihashimoto

:tada: This issue has been resolved in version 2.1.5 :tada:

The release is available on npm package (@latest dist-tag)

Your semantic-release bot :package::rocket:

github-actions[bot] avatar Jul 18 '24 14:07 github-actions[bot]

:tada: This issue has been resolved in version 3.1.5 :tada:

The release is available on npm package (@latest dist-tag)

Your semantic-release bot :package::rocket:

github-actions[bot] avatar Jul 18 '24 14:07 github-actions[bot]

:tada: This issue has been resolved in version 2.1.5 :tada:

The release is available on npm package (@latest dist-tag)

Your semantic-release bot :package::rocket:

github-actions[bot] avatar Jul 18 '24 14:07 github-actions[bot]

:tada: This issue has been resolved in version 2.4.7 :tada:

The release is available on npm package (@latest dist-tag)

Your semantic-release bot :package::rocket:

github-actions[bot] avatar Jul 18 '24 14:07 github-actions[bot]

Thanks for the update. Cheers!

LeoLYW12138 avatar Jul 18 '24 15:07 LeoLYW12138