porsager / postgres

Postgres.js - The Fastest full featured PostgreSQL client for Node.js, Deno, Bun and CloudFlare
The Unlicense
7.39k stars 267 forks source link

TypeScript disallows interpolation helper for nested arrays that include null values #743

Open jxddk opened 10 months ago

jxddk commented 10 months ago

While attempting to perform multiple updates in one query, I encountered what I believe to be a TypeScript issue with the interpolation helper for nested arrays, when the lowest-level arrays include null values. My aim is to update multiple rows (including some values that may be null) in a single query, but the TypeScript error described below prevents me from doing so.

Please see the following TypeScript snippet, which I have run on Deno v1.35.2, using the v3.4.3 version of porsager/postgres. The final code block exemplifies the issue.

import postgres from 'npm:postgres@3.4.3'
const sql = postgres({})

// Calling helper function with an array of integers
console.log(await sql`SELECT * FROM (VALUES ${sql([1, 2, 3, 4])}) AS v(a, b, c, d)`)
// Result(1) [ { a: "1", b: "2", c: "3", d: "4" } ]

// Calling helper function with an array of arrays of integers
console.log(await sql`SELECT * FROM (VALUES ${sql([[1, 2, 3, 4]])}) AS v(a, b, c, d)`)
// Result(1) [ { a: "1", b: "2", c: "3", d: "4" } ]

// Calling helper function with an array including a null value
console.log(await sql`SELECT * FROM (VALUES ${sql([1, 2, null, 4])}) AS v(a, b, c, d)`)
// Result(1) [ { a: "1", b: "2", c: null, d: "4" }

// Calling helper function with an array of arrays including a null value
console.log(await sql`SELECT * FROM (VALUES ${sql([[1, 2, null, 4]])}) AS v(a, b, c, d)`)
// TS2769 [ERROR]: No overload matches this call.
// Overload 1 of 2, '(first: (number | null)[][] & readonly EscapableArray[]): Helper<(number | null)[][], []>', gave the following error.
//   Type 'null' is not assignable to type 'number'.  Overload 2 of 2, '(template: TemplateStringsArray, ...parameters: readonly ParameterOrFragment<never>[]): PendingQuery<Row[]>', gave the following error.
//   Type '[number, number, null, number]' is not assignable to type 'string'.

// Calling helper function with an array of arrays of strings
const names_set_a: [string, string, string][] = [
    ['Alicia', 'Beatrice', 'Clydeston'],
    ['Andrew', 'Bob', 'Cornwall'],
    ['Agnes', 'Bentley', 'Clifson'],
]
console.log(sql`
    UPDATE names SET
        middle=n.middle,
    FROM (VALUES ${sql(names_set_a)})
    AS n(
        first,
        middle,
        last,
    )
    WHERE n.first = first AND n.last = last
`)

// Calling helper function with an array of arrays of strings including nulls
const names_set_b: [string, null, string][] = [
    ['Andrew', null, 'Cornwall'],
    ['Alicia', null, 'Clydeston'],
    ['Agnes', null, 'Clifson'],
]
console.log(sql`
    UPDATE names SET
        middle=n.middle,
    FROM (VALUES ${sql(names_set_b)})
    AS n(
        first,
        middle,
        last,
    )
    WHERE n.first = first AND n.last = last
`)
// TS2769 [ERROR]: No overload matches this call.
//   Overload 1 of 2, '(first: [string, null, string][] & readonly EscapableArray[]): Helper<[string, null, string][], []>', gave the following error.
//     Argument of type '[string, null, string][]' is not assignable to parameter of type '[string, null, string][] & readonly EscapableArray[]'.
//       Type '[string, null, string][]' is not assignable to type 'readonly EscapableArray[]'.
//         Type '[string, null, string]' is not assignable to type 'EscapableArray'.
//           Type 'string | null' is not assignable to type 'string | number'.
//             Type 'null' is not assignable to type 'string | number'.  Overload 2 of 2, '(template: TemplateStringsArray, ...parameters: readonly ParameterOrFragment<never>[]): PendingQuery<Row[]>', gave the following error.
//     Argument of type '[string, null, string][]' is not assignable to parameter of type 'TemplateStringsArray'.
//       Property 'raw' is missing in type '[string, null, string][]' but required in type 'TemplateStringsArray'.

I will attempt to diagnose (and if possible, fix) this issue, and am posting this here in case there are any clear pointers, easy fixes, or different approaches that I may have missed. Thanks in advance.

baileywickham commented 7 months ago

We also hit this issue but with a different type, in our case a Buffer.