Skip to content

Commit c8754b7

Browse files
committed
fix(types): overrideTypes work on invalid embeded relation
When a relation doesn't exist, or that type inference fail to resolve it the new type should be the source of truth for the result
1 parent 9b2c8fc commit c8754b7

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

src/types.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ type Simplify<T> = T extends object ? { [K in keyof T]: T[K] } : T
131131
type MergeDeep<New, Row> = {
132132
[K in keyof New | keyof Row]: K extends keyof New
133133
? K extends keyof Row
134-
? // Check if the override is on a embeded relation (array)
134+
? Row[K] extends SelectQueryError<string>
135+
? New[K]
136+
: // Check if the override is on a embeded relation (array)
135137
New[K] extends any[]
136138
? Row[K] extends any[]
137139
? Array<Simplify<MergeDeep<NonNullable<New[K][number]>, NonNullable<Row[K][number]>>>>

test/override-types.test-d.ts

+52
Original file line numberDiff line numberDiff line change
@@ -435,3 +435,55 @@ const postgrest = new PostgrestClient<Database>(REST_URL)
435435
>
436436
>(true)
437437
}
438+
439+
// Test overrideTypes single object with error embeded relation
440+
{
441+
const result = await postgrest.from('users').select('*, somerelation(*)').overrideTypes<
442+
{
443+
somerelation: { created_at: Date; data: string }
444+
}[]
445+
>()
446+
if (result.error) {
447+
throw new Error(result.error.message)
448+
}
449+
let data: typeof result.data
450+
expectType<
451+
TypeEqual<
452+
typeof data,
453+
{
454+
username: string
455+
data: CustomUserDataType | null
456+
age_range: unknown
457+
catchphrase: unknown
458+
status: 'ONLINE' | 'OFFLINE' | null
459+
somerelation: { created_at: Date; data: string }
460+
}[]
461+
>
462+
>(true)
463+
}
464+
465+
// Test overrideTypes array object with error embeded relation
466+
{
467+
const result = await postgrest.from('users').select('*, somerelation(*)').overrideTypes<
468+
{
469+
somerelation: { created_at: Date; data: string }[]
470+
}[]
471+
>()
472+
if (result.error) {
473+
throw new Error(result.error.message)
474+
}
475+
let data: typeof result.data
476+
expectType<
477+
TypeEqual<
478+
typeof data,
479+
{
480+
username: string
481+
data: CustomUserDataType | null
482+
age_range: unknown
483+
catchphrase: unknown
484+
status: 'ONLINE' | 'OFFLINE' | null
485+
somerelation: { created_at: Date; data: string }[]
486+
}[]
487+
>
488+
>(true)
489+
}

0 commit comments

Comments
 (0)