supabase/postgres-meta

Incorrect typescript types generated for functions returning records or table row types (generated as unknown)

Opened this issue · 3 comments

Describe the bug
If a postgres function returns a record or a table row type, the generated return type is "unknonwn" which is incorrect.

To Reproduce

  1. Create a function which returns a record or a table row type.
  2. Generate types (supabase gen types typescript --project-id "<project_id>" --schema <schema_id> > supabase/types/supabase.ts")

Expected behavior
It should return the table row type or record type or as a workarround a value which is not "unknown". The return type "unknown" removes type safety but also complicates working with the returned data.

Code snippets
If I have a table

CREATE TABLE (
    id  uuid,
    name text
)

and I create a function returning a record of that table

CREATE FUNCTION read_group_record(group_id uuid)
    RETURNS record
DECLARE
    group_record record;
BEGIN
    SELECT *
    INTO group_record
    FROM
        groups
    WHERE
        groups.id = group_id;
    RETURN group_record;
END

or a table row type of that table

CREATE FUNCTION read_group_table_row_type(group_id uuid)
    RETURNS groups
DECLARE
    group_record groups;
BEGIN
    SELECT *
    INTO group_record
    FROM
        groups
    WHERE
        groups.id = group_id;
    RETURN group_record;
END

the generated types are:

      read_group_record: {
        Returns: Record<string, unknown>
      }
      read_group_table_row_type: {
        Returns: unknown
      }

But I would have expected something like

      read_group_table_row_type: {
        Returns: {
           id: string
           name: string
        }
      }

System information

  • Version of OS: Windows 11
  • Version of CLI: v1.163.6
  • Version of Docker: Docker Desktop 4.24.2 (124339)
  • Versions of services: [Generated types | Output command generate types]

Additional context

  • Browser [e.g. edge, chrome]

PS: This is my first bug report. If you require more information, I am happy to provide them. If I should define the return types differently to use the type generation, please provide me an example and I will close the issue.

We ran into this issue as well using the "like" syntax: RETURNS TABLE (LIKE <table_name>), which, when the rpc is called, returns data as

const data: {
    like: unknown;
} | null

Same issue for functions like this, field music will have unknown type in the result

RETURNS TABLE (
  music public.music, 
  username TEXT
)

Same issue here