TS error when trying to wrap an update call #676
-
I have a builder pattern which wraps the Supabase API to provide a table-specific API and using the following code I get an unexpected typescript error which I don't really understand. import { SupabaseClient } from "@supabase/supabase-js";
import type { PostgrestQueryBuilder } from "@supabase/postgrest-js";
export type IUpdateOptions<T> = Parameters<PostgrestQueryBuilder<T>["update"]>[1];
export type IUpdateReturn<T> = ReturnType<PostgrestQueryBuilder<T>["update"]>;
export type IUpdate<T> = (
/** a set of properties on the _given table_ which the user wants to **update**. */
updated: Partial<T>,
/** optional options for the update operation */
options?: IUpdateOptions<T>
) => IUpdateReturn<T>;
/**
* A higher-order function which allows configuration of a specific table and then
* exposes the `update()` API which has been isolated to the given table's info.
*/
export const updateDefinition = <T>(
/** The Postgres table name */
tableName: string,
/** the API client */
client: SupabaseClient
): IUpdate<T> => {
return (updated, options) => {
return client.from<T>(tableName).update(updated, options);
};
}; Unfortunately Typescript complains that:
Interestingly, a similar wrapper around select does not produce this error: import type { PostgrestFilterBuilder } from "@supabase/postgrest-js";
import type { SupabaseClient } from "@supabase/supabase-js";
export type ISelect<T> = (...columns: ["*"] | Array<keyof T>) => PostgrestFilterBuilder<T>;
export function selectDefinition<T>(tableName: string, client: SupabaseClient): ISelect<T> {
return (...columns: ["*"] | Array<keyof T>) => {
const cols =
columns.length === 0 || (columns.length === 1 && columns[0]) === "*"
? "*"
: columns.join(",");
// receives a _typed_ filter operator which can
const filter = (client.from<T>(tableName).select(cols) as unknown) as PostgrestFilterBuilder<T>;
return filter;
};
} Note: while in the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I'm going to close my own issue ... seems there were inconsistencies in supabase versions in the monorepo I was using. |
Beta Was this translation helpful? Give feedback.
I'm going to close my own issue ... seems there were inconsistencies in supabase versions in the monorepo I was using.