A todo list screen. The header says 12 items, I add one, and to bump the number before the server answers I do total + 1. The header now says 121 items. (For a second I thought a hundred todos had appeared out of nowhere.)
Here's why. PostgreSQL's count(*) is a bigint, and the Node pg driver returns bigint as a string by default, since a JavaScript number can't hold every 64-bit integer. And the row type is something I wrote by hand.
type CountRow = { total: number };
const { rows } = await pool.query<CountRow>(
'SELECT count(*) AS total FROM todos WHERE done = false',
);
setTotal(rows[0].total + 1);
The type says number, so the editor stays quiet, and at run time '12' + 1 is string concatenation.
I can see two fixes. Cast in SQL with count(*)::int, or parse the value in code. With only a few queries the cast looks easier. Where do you all put it?