From 1c139c727399e83bf0ceda29ff3101ca21050dac Mon Sep 17 00:00:00 2001 From: avallete Date: Wed, 1 Oct 2025 15:02:07 +0200 Subject: [PATCH 1/2] fix(typescript): unknown is already nullable Fixes: https://github.com/supabase/cli/issues/4234 https://github.com/supabase/cli/issues/577 --- src/server/templates/typescript.ts | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/server/templates/typescript.ts b/src/server/templates/typescript.ts index 0c91976d..c361409f 100644 --- a/src/server/templates/typescript.ts +++ b/src/server/templates/typescript.ts @@ -482,6 +482,14 @@ export const apply = async ({ }` : '' + function generateNullableUnionTsType(tsType: string, isNullable: boolean) { + // Only add the null union if the type is not unknown as unknown already includes null + if (tsType === 'unknown' || !isNullable) { + return tsType + } + return `${tsType} | null` + } + function generateColumnTsDefinition( schema: PostgresSchema, column: { @@ -497,7 +505,7 @@ export const apply = async ({ views: PostgresView[] } ) { - return `${JSON.stringify(column.name)}${column.is_optional ? '?' : ''}: ${pgTypeToTsType(schema, column.format, context)} ${column.is_nullable ? '| null' : ''}` + return `${JSON.stringify(column.name)}${column.is_optional ? '?' : ''}: ${generateNullableUnionTsType(pgTypeToTsType(schema, column.format, context), column.is_nullable)}` } let output = ` @@ -537,7 +545,7 @@ export type Database = { ...schemaFunctions .filter(({ fn }) => fn.argument_types === table.name) .map(({ fn }) => { - return `${JSON.stringify(fn.name)}: ${getFunctionReturnType(schema, fn)} | null` + return `${JSON.stringify(fn.name)}: ${generateNullableUnionTsType(getFunctionReturnType(schema, fn), true)}` }), ]} } @@ -610,7 +618,7 @@ export type Database = { .filter(({ fn }) => fn.argument_types === view.name) .map( ({ fn }) => - `${JSON.stringify(fn.name)}: ${getFunctionReturnType(schema, fn)} | null` + `${JSON.stringify(fn.name)}: ${generateNullableUnionTsType(getFunctionReturnType(schema, fn), true)}` ), ]} } @@ -709,12 +717,15 @@ export type Database = { const type = typesById.get(type_id) let tsType = 'unknown' if (type) { - tsType = `${pgTypeToTsType(schema, type.name, { - types, - schemas, - tables, - views, - })} | null` + tsType = `${generateNullableUnionTsType( + pgTypeToTsType(schema, type.name, { + types, + schemas, + tables, + views, + }), + true + )}` } return `${JSON.stringify(name)}: ${tsType}` })} From 4a1cdb6fd0502605029e3830a8880b853a313d2e Mon Sep 17 00:00:00 2001 From: avallete Date: Wed, 1 Oct 2025 15:13:10 +0200 Subject: [PATCH 2/2] fix: also exclude any from null union --- src/server/templates/typescript.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/server/templates/typescript.ts b/src/server/templates/typescript.ts index c361409f..1b527686 100644 --- a/src/server/templates/typescript.ts +++ b/src/server/templates/typescript.ts @@ -484,7 +484,7 @@ export const apply = async ({ function generateNullableUnionTsType(tsType: string, isNullable: boolean) { // Only add the null union if the type is not unknown as unknown already includes null - if (tsType === 'unknown' || !isNullable) { + if (tsType === 'unknown' || tsType === 'any' || !isNullable) { return tsType } return `${tsType} | null`