You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The return type from pool.query (or client.query) is QueryResult, which is incorrect when multiple statements are executed. In that case the type is QueryResult[].
Example showing the problem:
importpgfrom'pg'// docker run --rm -p 5432:5432 -e POSTGRES_PASSWORD=postgres postgres:15-alpineconstpool=newpg.Pool({connectionString: 'postgresql://postgres:postgres@localhost:5432/postgres'})constresults=awaitpool.query(` SELECT 1; SELECT 2;`)console.log(results[1].rows)// TS ERROR: 'QueryResult<any>' cannot be indexed. It is not an array.
But the code runs as expected.
I think this pattern of making the object an array or an object is not so good. The correct type right now is QueryResult | QueryResult[] which is difficult to work with since one would need to check if (Array.isArray(result)) each type to do type narrowing.
I suggest that we make the following (mostly) backwards compatible change: Change the return type of query to QueryResult & QueryResult[]. It may look strange but it is fully doable in both javascript and typescript; an array that also has object properties.
The return type from
pool.query
(orclient.query
) isQueryResult
, which is incorrect when multiple statements are executed. In that case the type isQueryResult[]
.Example showing the problem:
But the code runs as expected.
I think this pattern of making the object an array or an object is not so good. The correct type right now is
QueryResult | QueryResult[]
which is difficult to work with since one would need to checkif (Array.isArray(result))
each type to do type narrowing.I suggest that we make the following (mostly) backwards compatible change: Change the return type of
query
toQueryResult & QueryResult[]
. It may look strange but it is fully doable in both javascript and typescript; an array that also has object properties.Proof of concept code:
The text was updated successfully, but these errors were encountered: