Skip to content

Commit

Permalink
fix(types): resolve arrays correctly with multi-property resolver (#295)
Browse files Browse the repository at this point in the history
  • Loading branch information
LiamMartens committed Oct 7, 2022
1 parent 3f34410 commit a422167
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/query/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ type ResolveFieldType<T> = NonNullable<T> extends Record<string, any>
: ResolverAction<NonNullable<T>>

interface ResolverFunction<T, Arr = false> {
<P extends keyof T>(props: P[]): ResolverAction<Pick<T, P>>
<P extends keyof T>(props: P[]): Arr extends true
? ResolveFieldType<Array<Pick<T, P>>>
: ResolverAction<Pick<T, P>>
}
interface ResolverFunction<T, Arr = false> {
<P extends keyof T>(prop: P): Arr extends true
Expand Down
1 change: 0 additions & 1 deletion test/builder.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import groq from 'groq'
import { describe, test, expect } from 'vitest'

import { defineDocument, defineFields } from 'sanity-typed-queries'

const { author } = defineDocument('author', {
Expand Down
7 changes: 7 additions & 0 deletions test/types/builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,13 @@ describe('builder types', () => {
.first()
.use()[1]
).toEqualTypeOf<(string[] | undefined)[] | undefined>()
expectTypeOf(
referenceBuilder
.map(r => ({ fields: r.authors.resolveIn(['_id', 'more']).use() }))
.pick('fields')
.first()
.use()[1]
).toEqualTypeOf<Array<{ _id: string; more?: string[] }> | undefined>()

const subqueryType = defineDocument('test', { title: { type: 'string' } })
.builder.subquery({
Expand Down

0 comments on commit a422167

Please sign in to comment.