postgrest-js
postgrest-js copied to clipboard
Typescript - Fetched data is type is not being inferred correctly based on injected data types
Bug report
Describe the bug
When querying the database context in Typescript, the data returned is always of type any
instead inferring the correct type from the injected database types. Previously, the correct types would be returned in the data object.
To Reproduce
Steps to reproduce the behavior, please provide code snippets or a repository:
- Create a database context using type injection as documented here
- Using the context, query a table with a know injected type
const { data, error } = supabaseClient.from('authors').select()
- Observe returned
data
object type
Expected behavior
The expected behavior, and what previously happened, is that the data
object type would be correctly inferred as an Author[]
object based on injected data types. Instead the data
object is of type `any[]
Screenshots
System information
- OS: MacOS 13.0.1
- Version of supabase-js: 2.8.0
- Version of Node.js: 16.16.0
I've got the exact same issue:

I've noticed the various PostgrestBuilder objects aren't noticing the typings, however the supabase client object is:

Oddly enough, if I specify the columns manually the types carry over fine, but not when using the wildcard *

Any insight would be greatly appreciated.
Same here. Doesn't work with the Supabase docs' suggestions either.
const supabase = createClient<Database>(supabaseUrl, supabaseKey);
export function modelStoreFactory<T extends keyof Database['public']['Tables']>(tableName: string) {
const tbl = supabase.from(tableName);
type TRow = Database['public']['Tables'][T]['Row'];
type TInsert = Database['public']['Tables'][T]['Insert'];
async function insertItem(item: TInsert): Promise<TRow> {
const result: PostgrestSingleResponse<TRow> = await tbl.insert(item).select();
}
}
Result:
1. Type 'PostgrestSingleResponse<any[]>' is not assignable to type 'PostgrestSingleResponse<TRow>'.
Type 'PostgrestResponseSuccess<any[]>' is not assignable to type 'PostgrestSingleResponse<TRow>'.
Type 'PostgrestResponseSuccess<any[]>' is not assignable to type 'PostgrestResponseSuccess<TRow>'.
Type 'any[]' is not assignable to type 'TRow'.
Edit: The Supabase JS Client docs were not super clear on this, but I fixed the error in this case by using .limit(1).single()
to return only one updated object, as I expected.