Search code examples
typescriptnext.jssupabase

Implementing Supabase Typescript Types


I am trying to use Supabase typescript types in my Next.js project, but I get the error Expected 0 type arguments, but got 1.

I am following this documentation. I have generated the types just fine, but I can't use them in my app.

When I try to use this code:

import { createClient } from "@/utils/supabase/server";
import { Database } from "@/types/supabase";

export async function myFunction() {
  const supabase = createClient<Database>();
}

The <Database> gives me this error: Expected 0 type arguments, but got 1.. If I take out <Database> I get no error, but I then lose my typesafety


Solution

  • Try this:

    import { Database } from '@/types/supabase'
    import { createServerClient, type CookieOptions } from '@supabase/ssr'
    import { cookies } from 'next/headers'
    
    export const createClient = () => {
     => {
      const cookieStore = cookies()
      return createServerClient<Database>(
        process.env.SUPABASE_URL!,
        {
          cookies: {
            get(name: string) {
              return cookieStore.get(name)?.value
            },
            set(name: string, value: string, options: CookieOptions) {
              try {
                cookieStore.set({ name, value, ...options })
              } catch (error) {
                // The `set` method was called from a Server Component.
                // This can be ignored if you have middleware refreshing
                // user sessions.
              }
            },
            remove(name: string, options: CookieOptions) {
              try {
                cookieStore.set({ name, value: '', ...options })
              } catch (error) {
                // The `delete` method was called from a Server Component.
                // This can be ignored if you have middleware refreshing
                // user sessions.
              }
            },
          },
        }
      )
    }