Skip to content

Commit

Permalink
Fix function return type incorrectly being never
Browse files Browse the repository at this point in the history
  • Loading branch information
luminamystere committed Jan 2, 2025
1 parent b71e10c commit 7070d43
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/FunctionCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,27 @@ import { VirtualTable } from "./VirtualTable";
export type FunctionOutput<SCHEMA extends DatabaseSchema, FUNCTION extends FunctionSchema, FUNCTION_NAME extends DatabaseSchema.FunctionName<SCHEMA>> =
FUNCTION extends FunctionSchema<(TypeString | OptionalTypeString)[], infer OUT, infer RETURN> ?
{ [I in keyof OUT as OUT[I] extends [TypeString, infer NAME extends PropertyKey] ? NAME : never]: OUT[I] extends [infer TYPE extends TypeString, string] ? TYPE : never } extends infer OUT_COLUMNS ?
RETURN extends `SETOF ${infer TABLE_NAME extends DatabaseSchema.TableName<SCHEMA>}` ?
DatabaseSchema.Table<SCHEMA, TABLE_NAME> extends infer TABLE ?

OUT["length"] extends 0 ? TABLE
: { [KEY in keyof TABLE | keyof OUT_COLUMNS]: KEY extends keyof OUT_COLUMNS ? OUT_COLUMNS[KEY] : KEY extends keyof TABLE ? Extract<TABLE[KEY], TypeString> : never }

: never
: (OUT["length"] extends 0 ? { [KEY in FUNCTION_NAME]: RETURN } : "Error! Function return type must be a table if there are OUT parameters")
: never
(
(
RETURN extends `SETOF ${infer TABLE_NAME extends DatabaseSchema.TableName<SCHEMA>}` ?
(DatabaseSchema.Table<SCHEMA, TABLE_NAME> extends infer TABLE ? TABLE : never)

: RETURN extends `SETOF ${infer TYPE_NAME extends DatabaseSchema.TypeName<SCHEMA>}` ?
(DatabaseSchema.Type<SCHEMA, TYPE_NAME> extends infer TYPE ? TYPE : never)

: never
) extends infer TABLE ?

[TABLE] extends [never] ?
(OUT["length"] extends 0 ? { [KEY in FUNCTION_NAME]: RETURN } : "Error! Function return type must be a table if there are OUT parameters")
: (
OUT["length"] extends 0 ? TABLE
: { [KEY in keyof TABLE | keyof OUT_COLUMNS]: KEY extends keyof OUT_COLUMNS ? OUT_COLUMNS[KEY] : KEY extends keyof TABLE ? Extract<TABLE[KEY], TypeString> : never }
)
: never
)
: never
: never;

class FunctionCall<FUNCTION extends FunctionSchema, SCHEMA extends DatabaseSchema, FUNCTION_NAME extends DatabaseSchema.FunctionName<SCHEMA>, OUTPUT extends TableSchema = Extract<FunctionOutput<SCHEMA, FUNCTION, FUNCTION_NAME>, TableSchema>> extends VirtualTable<OUTPUT, never> {

Expand Down

0 comments on commit 7070d43

Please sign in to comment.