Skip to content

Generate text[] as (string | null)[] #613

@Jurajzovinec

Description

@Jurajzovinec

Postgres qualifies array of nullable strings to be typeof text[]

Image

But pgtyped generates (string)[], see:

.
.
.

example.sql

/* @name example */
SELECT pg_typeof(:input!::text[]);

Generates to:

example.queries.ts

/** Types generated for queries found in "src/report/upload/queries/example.queries.sql" */
import { PreparedQuery } from '@pgtyped/runtime';

export type stringArray = (string)[];

/** 'Example' parameters type */
export interface IExampleParams {
  input: stringArray;  // 👈 Type discrepancy, PG supports NULL as an element too.
}

/** 'Example' return type */
export interface IExampleResult {
  pg_typeof: unknown | null;
}

/** 'Example' query type */
export interface IExampleQuery {
  params: IExampleParams;
  result: IExampleResult;
}

const exampleIR: any = {"usedParamSet":{"input":true},"params":[{"name":"input","required":true,"transform":{"type":"scalar"},"locs":[{"a":17,"b":23}]}],"statement":"SELECT pg_typeof(:input!::text[])"};

/**
 * Query generated from SQL:
 * ```
 * SELECT pg_typeof(:input!::text[])
 * ```
 */
export const example = new PreparedQuery<IExampleParams,IExampleResult>(exampleIR);

As you can see, the generated input parameter type is problematic because it prevents us to send nullable values as parameters, although it is supported by Postgres. Having the (string)[] type for :input! parameter (and not (string | null)[]) forces users to violate TS

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions