supabase / postgrest-js

Isomorphic JavaScript client for PostgREST.
https://supabase.com
MIT License
965 stars 129 forks source link

Javascript RPC Typescript error: Argument of type 'string' is not assignable to parameter of type... #420

Open hichana opened 1 year ago

hichana commented 1 year ago

Bug report

Describe the bug

In the Javascript documentation an example is given for using rpc to call custom functions: https://supabase.com/docs/reference/javascript/using-filters const { data, error } = await supabase.rpc('hello_world')

In typescript, the following error is thrown: Argument of type 'string' is not assignable to parameter of type 'never'.ts(2345)

To mitigate the error I had to add a custom typescript definition for the supabase client:

// supabaseCustom.d.ts

import { PostgrestBuilder } from '@supabase/postgrest-js';
import { SupabaseClient } from '@supabase/supabase-js';

declare module '@supabase/supabase-js' {
  interface SupabaseClient {
    rpc<ResponseType, ParamsType>(
      fn: string,
      params?: ParamsType
    ): PostgrestBuilder<ResponseType>;
  }
}

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. attempt to use this code const { data, error } = await supabase.rpc('hello_world')
  2. Expected behavior

There should be no ts error

soedirgo commented 1 year ago

Hi, can you put up a repo so I can reproduce the issue? We have a test here which doesn't have the issue above, so not sure how that happens.

abeisleem commented 9 months ago

Experiencing the same issue using: "@supabase/auth-helpers-nextjs": "^0.7.4", "@supabase/auth-ui-shared": "^0.1.7", "@supabase/supabase-js": "^2.33.2",

Specifically, I'm using the createClientComponentClient from "@supabase/auth-helpers-nextjs"

abeisleem commented 9 months ago

@hichana ... I am no longer facing this issue. Make sure your generated types are up-to-date with the database. Generate with supabase gen types typescript --project-id abcdefghijklmnopqrst > database.types.ts (or without --project-id if local). This will create a hello_world Function for you database.types.ts. I'm assuming you're initializing your supabase client with the Database type (ex. createClient<Database>(...). This is what's causing that error since your database.types.ts is not up-to-date with the db.

ajesuscode commented 5 months ago

I can confirm @abeisleem you provided correct solution, thank you!

zillBoy commented 1 week ago

Hi, I believe this issue can be closed. The solution provided by @abeisleem is working fine, thanks for confirming the fix @ajesuscode.

For me, I used the supabase website to generate the types Link: https://supabase.com/docs/guides/api/rest/generating-types

Screenshot_3300