Open
Description
The supabase gen types
appears to lack support for RPC functions returning objects with nullable properties.
Steps to reproduce:
Define an RPC function:
CREATE TYPE status_type_enum AS ENUM ('rotten', 'ripe');
CREATE FUNCTION get_bananas(
_limit INT DEFAULT NULL,
_offset INT DEFAULT 0
)
RETURNS TABLE (
id UUID,
text TEXT,
current_status status_type_enum -- Is there a way to make this nullable in TS?
)
LANGUAGE plpgsql
AS $$
BEGIN
RETURN QUERY
SELECT
b.id,
b.text,
s.status_type AS current_status
FROM bananas b
LEFT JOIN statuses s ON b.id = s.banana_id AND s.user_id = auth.uid()
LIMIT _limit
OFFSET _offset;
END;
$$;
Run the command:
npx supabase gen types
Examine the generated types:
It doesn't seem possible to make current_status
nullable out of the box, even though it can be missing.
get_bananas: {
Args: {
_limit?: number;
_offset?: number;
};
Returns: {
id: string;
text: string;
current_status: Database['public']['Enums']['status_type_enum'];
// current_status: null | Database['public']['Enums']['status_type_enum'];
}[];
};
Workaround
Extend the generated types from another file to overwrite properties.
Metadata
Metadata
Assignees
Labels
No labels
Activity
Marviel commentedon Jan 4, 2024
One thing that changing this will likely do is affect a lot of people's code.
(i.e. types that were previously non-null are now suddenly going to throw a lot of typescript compile errors)
So it may be wise to have this behind some sort of feature flag, i.e.
--rpc-allow-null-results
or something like that.wmarrujo commentedon Oct 4, 2024
here's my workaround:
use the
PartialNullBy
type for supabase to wrap the object part of what you get back. I've included theArrayElement
type so you can apply it to the object part of the type when you're getting a whole array (table) of objects. I also included aPartialBy
just for completeness, which will do the same thing using undefined instead.now, the resultant type will look gross, but you can wrap the whole thing with
Debug
to check that it evaluated correctlyDonnerstagnacht commentedon Apr 17, 2025
Same issue here. There is a stackoverflow question which suggests a solution using a reference to the table type.
https://stackoverflow.com/questions/77427669/supabase-rpc-typescript-types-nullable
Is that still a working solution @dshukertjr ? I was not able to make that solution work.
A simpler test example:
table definition:
postgres function:
generated types:
It is also a bit strange that the args object includes
{ user_id: string }
as the postgres argument is int.The correct type would be identical to the row type:
compare generated table row type:
windows 11 docker desktop
command: supabase gen types typescript --local > <file_name>
cli version: supabase -v 2.20.12
ngasull commentedon Apr 24, 2025
PostgreSQL primitive types are nullable, unless defined as domains which are NOT NULL. Currently, I think most generated types are inaccurate in this sense and that, in your case,
current_status
should include| null
without changing your code.☝ currently tracked in #842
Changing this behavior would obviously be a breaking change.