FormidableLabs / groqd

A schema-unaware, runtime and type-safe query builder for GROQ.
https://commerce.nearform.com/open-source/groqd
MIT License
221 stars 17 forks source link

How to type `default` within a select #271

Open b3nk3 opened 4 months ago

b3nk3 commented 4 months ago

Is there an existing issue for this?

Code of Conduct

Question

Hey there, I am using a `select` for my portable text content block and am running into issues, when trying to type `default`.

const unsupportedTypeBlockSelection = {
  _key: q.string(),
  _type: ['"unsupported"', q.literal('unsupported')],
  unsupportedType: ['_type', q.string()],
};

export type UnsupportedBlockType = TypeFromSelection<
  typeof unsupportedTypeBlockSelection
>;

I've been getting an error for typing the unsupported type:

Type '(string | ZodLiteral<"unsupported">)[]' is not assignable to type '[string, ZodType<any, ZodTypeDef, any>]'.
        Target requires 2 element(s) but source may have fewer.ts(2344)

My goal is to have a strict type in my SanityContent component

type ContentProps = {
  value: (PortableTextBlock | CodeBlockType | ImageBlockType | UnsupportedBlockType )[];
};

I can of course get around this by using TypedObject from @portabletext/types but that defies the point of strongly typing.