0

New to Typescript. I am struggling to figure out how to access and use the interface values in my database types export as interfaces in my code.

I don't want to create new seperate interfaces if my database types export already provides them.

Here is a sample of the interface export from Supabase in my 'types.ts' file:

export interface Database {
  graphql_public: {
    Tables: {
      [_ in never]: never
    }
    Views: {
      [_ in never]: never
    }
    Functions: {
      graphql: {
        Args: {
          operationName?: string
          query?: string
          variables?: Json
          extensions?: Json
        }
        Returns: Json
      }
    }
    Enums: {
      [_ in never]: never
    }
    CompositeTypes: {
      [_ in never]: never
    }
  }
  public: {
    Tables: {
      expenses: {
        Row: {
          amount: number
          category: string
          created_at: string | null
          date: string
          description: string | null
          file_names: string[] | null
          id: number
        }
        Insert: {
          amount: number
          category: string
          created_at?: string | null
          date: string
          description?: string | null
          file_names?: string[] | null
          id?: never
        }
        Update: {
          amount?: number
          category?: string
          created_at?: string | null
          date?: string
          description?: string | null
          file_names?: string[] | null
          id?: never
        }
        Relationships: [
          {
            foreignKeyName: "expenses_organisation_id_fkey"
            columns: ["organisation_id"]
            referencedRelation: "organisations"
            referencedColumns: ["id"]
          },
          {
            foreignKeyName: "expenses_uuid_fkey"
            columns: ["uuid"]
            referencedRelation: "profiles"
            referencedColumns: ["uuid"]
          },
          {
            foreignKeyName: "expenses_vehicle_id_fkey"
            columns: ["vehicle_id"]
            referencedRelation: "vehicles"
            referencedColumns: ["id"]
          }
        ]
      }
      logs: {
        Row: {
          created_at: string | null
          error: string | null
          function: string | null
          id: number
          value: number | null
        }
        Insert: {
          created_at?: string | null
          error?: string | null
          function?: string | null
          id?: number
          value?: number | null
        }
        Update: {
          created_at?: string | null
          error?: string | null
          function?: string | null
          id?: number
          value?: number | null
        }
        Relationships: []
      }
      notification_preferences: {
        Row: {
          function_name: string
          id: number
          organisation_id: number
          vehicle_id: number
        }
        Insert: {
          function_name: string
          id?: number
          organisation_id: number
          vehicle_id: number
        }
        Update: {
          function_name?: string
          id?: number
          organisation_id?: number
          vehicle_id?: number
        }
        Relationships: [
          {
            foreignKeyName: "notification_preferences_organisation_id_fkey"
            columns: ["organisation_id"]
            referencedRelation: "organisations"
            referencedColumns: ["id"]
          },
          {
            foreignKeyName: "notification_preferences_vehicle_id_fkey"
            columns: ["vehicle_id"]
            referencedRelation: "vehicles"
            referencedColumns: ["id"]
          }
        ]
      }
      notifications: {
        Row: {
          created_at: string
          dismissed_at: string | null
          emails: string[] | null
          emails_sent: boolean | null
        }
        Insert: {
          created_at?: string
          dismissed_at?: string | null
          emails?: string[] | null
          emails_sent?: boolean | null
        }
        Update: {
          created_at?: string
          dismissed_at?: string | null
          emails?: string[] | null
          emails_sent?: boolean | null
        }
        Relationships: [
          {
            foreignKeyName: "notifications_expense_id_fkey"
            columns: ["expense_id"]
            referencedRelation: "expenses"
            referencedColumns: ["id"]
          },
          {
            foreignKeyName: "notifications_organisation_id_fkey"
            columns: ["organisation_id"]
            referencedRelation: "organisations"
            referencedColumns: ["id"]
          },
          {
            foreignKeyName: "notifications_reminder_id_fkey"
            columns: ["reminder_id"]
            referencedRelation: "reminders"
            referencedColumns: ["id"]
          },
          {
            foreignKeyName: "notifications_vehicle_id_fkey"
            columns: ["vehicle_id"]
            referencedRelation: "vehicles"
            referencedColumns: ["id"]
          }
        ]
      }

I import the file.. import { Database } from "../_shared/interfaces.ts";

And would normall create an interface this way:

interface Expense {
  id: number;
  expense_category: string;
}

But I want to get it from the Database interface so I don't have to create it separately if that makes sense, for example:

interface Expense extends Database.public.expenses...

But nothing of this kind works, not sure if I am trying something that is not standard at all, but it seems that it should be? I am trying to use these interfaces in my Supabase functions.

Thank you

2
  • Try Database["public"]["expenses"]. Dot notation doesn't work for types, to subscript a type you need to use bracket notation. Also maybe you meant Database["public"]["Table"]["expenses"], because it seems to me that Database["public"] type doesn't have an expenses property. Commented Jul 10, 2023 at 8:38
  • 1
    @AlexChashin Works exactly this way, thank you! Didn't think it would be how it's done. reminder: Database['public']['Tables']['reminders']['Insert'], vehicle: Database['public']['Tables']['vehicles']['Insert'], - Can you post it as an answer so I can accept it? Commented Jul 10, 2023 at 22:31

1 Answer 1

0

Dot notation doesn't work for types, use bracket notation instead:

interface Expense extends Database["public"]["expenses"] {
  // ...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.