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
Database["public"]["expenses"]. Dot notation doesn't work for types, to subscript a type you need to use bracket notation. Also maybe you meantDatabase["public"]["Table"]["expenses"], because it seems to me thatDatabase["public"]type doesn't have anexpensesproperty.