#Supabase types broken?

25 messages · Page 1 of 1 (latest)

delicate sedge
#
import type { PostgrestResponse } from '@supabase/supabase-js';
import { getSupabase } from '@supabase/auth-helpers-sveltekit';
import type { Database } from '$lib/types/database.types';
import type { PageLoad } from './$types';
import { redirect } from '@sveltejs/kit';

type Message = Database['public']['Tables']['messages']['Row'];
type Profile = Database['public']['Tables']['profiles']['Row'];
type MessageWithProfile = Message & { profiles: Profile };

export const load = (async (event) => {
  const { session, supabaseClient } = await getSupabase(event);
  if (!session) {
    throw redirect(303, '/auth/login');
  }
  const { data: messages }: PostgrestResponse<MessageWithProfile> = await supabaseClient
    .from('messages')
    .select(
      `
    message,
    created_at,
    profiles (
    id,
    username
    )
  `
    )
    .eq('id', event.params.id);

  if (!messages) {
    return { messages: [] };
  }

  return { messages };
}) satisfies PageLoad;

Type 'PostgrestResponse<{ message: any; } & { created_at: any; } & { profiles: ({ id: any; } & { username: any; }) | ({ id: any; } & { username: any; })[] | null; }>' is not assignable to type 'PostgrestResponse<MessageWithProfile>'.
Type 'PostgrestResponseSuccess<{ message: any; } & { created_at: any; } & { profiles: ({ id: any; } & { username: any; }) | ({ id: any; } & { username: any; })[] | null; }>' is not assignable to type 'PostgrestResponse<MessageWithProfile>'.

pale bear
#

check your code

#

those types aren't compatible

#
// 1st part
{
    message: any;
    created_at: any;
    profiles: ({ id: any; } & { username: any; }) | ({ id: any; } & { username: any; })[] | null;
}

// 2nd part
Message & { profiles: Profile }
#

profiles could be an array and itit could also be null

#

but you don't handle those cases

#

also Message might be different from { message: any; created_at: any; }

#

@delicate sedge

delicate sedge
#

I'm reading it and re-reading it and I cant figure it out

#
       messages: {
        Row: {
          created_at: string;
          id: string;
          message: string;
          userId: string | null;
        };
#

@pale bear the error seems very different from the type/content I get:

<{ message: any; } & { created_at: any; } & { profiles: ({ id: any; }

#

seems like nested objects for each property

pale bear
#

it's an intersection

#

those properties can be combined together to make an object

#

nothing special
it's just because of how the library works, returning one property at a time, then merging them together

delicate sedge
#
[{"message":"Hi, my name is Sarah. I'm a software engineer from New York.","created_at":"2022-10-01T00:43:48+00:00","profiles":{"id":"df9a6200-562a-4d71-b702-cb3861237345","username":"gkatsanos"}}, 

#

this is my actual content

#

but why is it complaining its 'any' even though my type is declared as 'string'

#

the TS errors really dont make sense to me 😦

#
type MessageWithProfile = {
    created_at: string;
    id: string;
    message: string;
    userId: string;
} & {
    profiles: Profile;
}
#

this is my type ^

#

any idea what is missing there ?

#

ah ok, I was returning less fields than the fields declared in the type...

#

god I love chat gpt! 😄

pale bear
#

!resolved