Home > Net >  extending nested interface generated by supabase
extending nested interface generated by supabase

Time:09-22

I have the following Typescript interface generated by Supabase

export interface definitions {  
  Users: {
    /** Format: uuid */
    id: string;
    /**
     * Format: timestamp with time zone
     * @default now()
     */
    created_at?: string;
    /**
     * Format: uuid
     * @description Note:
     * This is a Primary Key.<pk/>
     */
  };
  Content: {
    /**
     * Format: uuid
     * @description Note:
     * This is a Primary Key.<pk/>
     */
    id: string;
    /**
     * Format: timestamp with time zone
     * @default now()
     */
    created_at?: string;
    /** Format: text */
    name: string;
    /** Format: text */
    description: string;
  };
}

This interface must not be modified as it is auto-generated.

I usually use it like this on queries:

import { definitions } from "../types/supabase";
const id = 1
let { data, error } = await supabase
  .from<definitions["Users"]>("Users").select("created_at")
  .eq("id", id);

However for this query I need to extend my interface:

let { data: Content, error } = await supabase
  .from<ContentAndUsers>("Content")
  .select(`*, Users (id, created_at)`)
  .eq("id", id);

I tried creating the interface but it gives me TS error:

interface UserContentCombo extends definitions["Content"] {
    ...definitions["Users"]
}

what is the correct syntax? Thanks

CodePudding user response:

If you want to have a type containing all the properties from both Users and Content, this would probably be the shortest way:

type UserContentCombo = definitions["Content"] & definitions["Users"]

Playground

CodePudding user response:

I see that the error says:

An interface can only extend an identifier/qualified-name with optional type arguments

So to give it an identifier and fix it you can define an intermediate type:

type userDefinitions = definitions['User'];

And then use that identifier:

interface UserContentCombo extends userDefinitions {}

No need of using the spread operator

TS Playground

  • Related