#A help with interfaces

8 messages · Page 1 of 1 (latest)

pine quest
#

Hello i have encountered a problem with interfaces and the way my data works

I have this object that needs types

user: {
    UserData,
    admin: AdminData
  } | null;

export type UserData = Database["public"]["Tables"]["profiles"]["Row"];
export type AdminData = Database["public"]["Tables"]["admins"]["Row"];

The thing is, i cannot set types for the top level data, or at least i dont know how to.

My data is as follows:

{
  id: '24020b3d-17ce-449e-846f-db12d6aeacee',
  updated_at: '2023-11-15T20:04:08.107+00:00',
  username: 'cooldude',
  full_name: 'Name Lastname',
  avatar_url: '24020b3d-17ce-449e-846f-db12d6aeacee-0.04009886646356353.png',
  website: 'www.website.lt',
  admin: {
    user_id: '24020b3d-17ce-449e-846f-db12d6aeacee',
    admin_from: '2023-11-19T16:12:17.945007+00:00',
    admin: true
  }
}

everything except from admin object is the type UserData, how can i make it work?

brisk wind
#

If I'm understanding correctly, you already have UserData and want to add admin to it, in which you case you can do (UserData & { admin: AdminData }) | null

pine quest
#

ah yes thanks

#

weird syntax

#

i thought it would be something like
{
...UserData;
admin: AdminData
}

#

!rev

#

!resolved

dense zealot
#

export type UserData = {
id: string;
updatedAt: string;
username: string;
fullName: string;
avatarUrl: string;
website: string;
};

export type AdminData = {
userId: string;
adminFrom: string;
admin: boolean;
};

export type UserDataOrAdminData = UserData | AdminData;

export type UserDataOrAdminDataWithUser = {
user: UserDataOrAdminData | null;
};