You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// tsx
declare type Member<A> = A extends readonly (infer T)[] ? T : never
const Link = ({ link }: { link: Member<Sanity.SomeDocQueryResultMemberLinks> }) => …
Even cooler would be the ability to add an alias to the array member…
// query:
*[_type == 'linkList' && slug.current == $slug][0]{
title,
links[] -> "link": { // < member alias
_id,
link
},
}
// generated code:
export type SomeDocQueryResult = {
title: string
links: Array<SomeDocQueryResultMemberLink>
}
export type SomeDocQueryResultMemberLink = {
_id: string
link: string
}
// tsx
const Link = ({ link }: { link: Sanity.SomeDocQueryResultMemberLink }) => …
… but this solution would require extending Groq itself, which I imagine you wouldn't take lightly.
Anyway, if there's an obvious solution to this that I'm missing, I'd love to hear about it. Otherwise, that's my suggestion.
Thank you for making this tool! It's been a rocky ride, but I would have had to move on from Sanity without it.
The text was updated successfully, but these errors were encountered:
Incidentally, yes, I do see some difficulty in the possibility of redundant aliases within individual queries and across multiple queries (hence naming like SomeDocQueryResultMemberLink in the examples)… IMO that can just be resolved with a huge type error in tyepgen, "all aliases must be unique", no need to overthink that.
… I did figure out one solution… if I were new to Typescript this would make me give up and join a road crew. Would join the garbage crew if it were deeply nested.
declare type Member<A> = A extends readonly (infer T)[] ? T : never
export const Link = ({ link }: {
link: Member<NonNullable<Sanity.PageQueryResult>['projects']>
}
I frequently find myself with a query that's something like this (ultra-simplified example)…
On the front end, I can pluck the type for links pretty easily…
But I haven't found an easy way to type a single member of the array.
It occurred to me that Sanity's use of projection aliases (I don't know if you have a technical term for that) could solve this.
For example, this could return a new type for the list array…
Then one could use the fun Member helper…
Even cooler would be the ability to add an alias to the array member…
… but this solution would require extending Groq itself, which I imagine you wouldn't take lightly.
Anyway, if there's an obvious solution to this that I'm missing, I'd love to hear about it. Otherwise, that's my suggestion.
Thank you for making this tool! It's been a rocky ride, but I would have had to move on from Sanity without it.
The text was updated successfully, but these errors were encountered: