Skip to content

Commit 950a6ff

Browse files
committedFeb 19, 2025
fix: preserve result optionality in override
1 parent bf0650b commit 950a6ff

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed
 

‎src/PostgrestBuilder.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ export default abstract class PostgrestBuilder<Result, ThrowOnError extends bool
219219
* Override the type of the returned `data`.
220220
*
221221
* @typeParam NewResult - The new result type to override with
222-
* @deprecated Use overrideTypes<yourType, { partial: false }>() method at the end of your call chain instead
222+
* @deprecated Use overrideTypes<yourType, { merge: false }>() method at the end of your call chain instead
223223
*/
224224
returns<NewResult>(): PostgrestBuilder<CheckMatchingArrayTypes<Result, NewResult>, ThrowOnError> {
225225
return this as unknown as PostgrestBuilder<

‎src/types.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import PostgrestError from './PostgrestError'
2+
import { ContainsNull } from './select-query-parser/types'
23
import { SelectQueryError } from './select-query-parser/utils'
34

45
export type Fetch = typeof fetch
@@ -110,4 +111,8 @@ export type CheckMatchingArrayTypes<Result, NewResult> =
110111
? {
111112
Error: 'Type mismatch: Cannot cast single object to array type. Remove Array wrapper from return type or make sure you are not using .single() up in the calling chain'
112113
}
113-
: NewResult // Neither are arrays - valid
114+
: // Neither are arrays - valid
115+
// Preserve the optionality of the result if the overriden type is an object (case of chaining with `maybeSingle`)
116+
ContainsNull<Result> extends true
117+
? NewResult | null
118+
: NewResult

‎test/returns.test-d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const postgrest = new PostgrestClient<Database>(REST_URL)
3131
throw new Error(maybeSingleResult.error.message)
3232
}
3333
let maybeSingleResultType: typeof maybeSingleResult.data
34-
let maybeSingleExpected: { username: string }
34+
let maybeSingleExpected: { username: string } | null
3535
expectType<TypeEqual<typeof maybeSingleResultType, typeof maybeSingleExpected>>(true)
3636

3737
// Test array to non-array type casting error

0 commit comments

Comments
 (0)
Please sign in to comment.