skip to Main Content

So i’m trying to use Supabase + typescript but when i want to use insert(), update(), upsert() and other functions im getting an error when i want to declare the object that i want to declare "Type <Restaurant> does not satisfy the constraint ‘never’. ts(2344)"

this is my code:

type Restaurant = {
    id: string;
    name: string;
    categories: string[];
    open: boolean;
    description: string;
  }


const addRestaurant = async () => {
    if (!user.value) return 
    
    const { data, error } = await supabaseClient
      .from('restaurants')
      .upsert<Restaurant>({
        id: user.value.id,
        name: name.value,
        categories: arr,
        open: status.value,
        description: description.value
      })
      .select()
      console.log(data, error);

  }

im getting that error on <Restaurant> but if i don’t specify the type of it i get almost the same error but saying:

Argument of type ‘{ id: string; name: string; categories: string; open: boolean; description: string; }’ is not assignable to parameter of type ‘never[]’.
Object literal may only specify known properties, and ‘id’ does not exist in type ‘never[]’.ts(2345)

also when i hover the "upsert()" function it looks like it’s using this on the background

PostgrestQueryBuilder<never, never>

how can i fix this?

  • i tried specifying the Type of the object but it appears that the functions [insert, update, upsert] are asking for something else.
  • i discovered that if i add a second parameter with an empty string the error goes away but if i edit something inside of it i get the error again (and that shouldn’t be the best way to fix it)
  • The code works with and without the second parameter, it’s just the error that im getting during development
    -also i don’t want to turn off a property of Typescript because i want to see when there is an error

2

Answers


  1. Chosen as BEST ANSWER

    To fix this problem all i had to do is to generate the types from Supabase, i did it by using the Supabase CLI and running this command on the console:

    supabase gen types typescript --project-id
    

    then i had to inject the types to my supabase client like this:

    import { createClient } from '@supabase/supabase-js'
    import { Database } from 'lib/database.types'
    
    const supabase = createClient<Database>(
      process.env.SUPABASE_URL,
      process.env.SUPABASE_ANON_KEY
    )
    

  2. With supabase-js this is not how you deal with types. Please have a read of the TypeScript support section of the documentation to understand this better. If you don’t specify a type there should be no error unless you are passing in values that are of type never[]. In your code user.value.id is never guaranteed, so the error you are getting is correct as you don’t have a guard clause against this.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search