porsager / porsager/postgres

TypeScript disallows interpolation helper for nested arrays that include null values

Open
#743 2 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Typescript
Dominant language
JavaScript
Stars
8.7k
Forks
374
Avg merge
11d 16h
Merged PRs (30d)
1

Description

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the sql() interpolation helper and its TypeScript overloads, using the nested-array examples with null values as the reproduction. Compile the Deno snippets to confirm the failure and verify that the nullable nested-array cases type-check while the existing non-null examples continue to work.

Written by the indexing model from the issue text.

Assessment

Tech stack
deno, postgres, typescript
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.