-
Notifications
You must be signed in to change notification settings - Fork 5
Build Helper for inferring field types #196
Comments
I'll shoot from the hip here about what I think is going on. When it's inline in the When you pulled it out into If that's the issue, it would be nice to have some kind of exported type from this library you could use to make this a little easier. Until then, make sure the types of your I haven't checked out your issue at all to see if this is the case. |
Thanks for response 🙏 |
Yeah, so Something like this would work, just so it ends up inferring the same type that document would: const sharedFields = <
Names extends string,
Zods extends z.ZodTypeAny,
ResolvedValues,
Optionals extends boolean,
FieldsArray extends TupleOfLength<
FieldOptions<Names, Zods, ResolvedValues, Optionals>,
1
>
>(
fields: FieldsArray
) => fields;
const fields = sharedFields([
{
name: "foo",
type: boolean(),
},
{
name: "bar",
optional: true,
type: string(),
},
]);
const type = document({
name: "foo",
fields,
}); Something I could export from the package, just haven't yet. |
I have a few places in my codebase where I'm defining shared fields in an array to map over and alter in the document definitions. When I reference those shared fields in the document definition I get the following type error:
`Type '{ name: string; type: SanityType<{ options: (Omit<StringOptions | undefined, "list"> & { list?: [string | { title: string; value: string; }, ...(string | { title: string; value: string; })[]] | undefined; }) | undefined; ... 6 more ...; placeholder?: string | undefined; }, string, string, string>; }[]' is not assignable to type '[FieldOptions<string, ZodTypeAny, unknown, boolean>, ...FieldOptions<string, ZodTypeAny, unknown, boolean>[]]'.
Source provides no match for required element at position 0 in target.`
Weird thing is if I access a shared field by index on the array no typeerror shows so its only iteration that makes this throw. I even tried exporting the FieldsArray type from but that type requires arguments I'm not sure how to make dynamic. Any help/feedback appreciated.
The text was updated successfully, but these errors were encountered: