Skip to content

Some PG number types do not marshal to a number #978

@awildeep

Description

@awildeep

Description

When selecting data into a variable defined by a Typescript type, not all number types properly marshal into typescript numbers.

Version: 3.4.5

Example code

import postgres from "postgres";

const sql = postgres({});

export interface PgNumbers {
    integer: number
    smallint: number
    bigint: number
    decimal: number
    numeric: number
    real: number
    doublePrecision: number
}

export const getNumberTypes = async (): Promise<Array<PgNumbers>> => {
    return sql<PgNumbers[]>`
    SELECT
        100::smallint as smallint,
        47483647::integer as integer,
        372036854775807::bigint as bigint,
        9223372036854.368547758::decimal as decimal,
        9223372036854.368547758::numeric as numeric,
        768.123192::real as real,
        30982137.9102379202::double precision as doublePrecision
        ;
  `
}
const numTypes=await (getNumberTypes())

console.log(numTypes[0])

Received:

 {
     smallint: 100,
     integer: 47483647,
     bigint: "372036854775807",
     decimal: "9223372036854.368547758",
     numeric: "9223372036854.368547758",
     real: 768.12317,
     doubleprecision: 30982137.91023792,
 }

Expected:

 {
     smallint: 100,
     integer: 47483647,
     bigint: 372036854775807,
     decimal: 9223372036854.368547758,
     numeric: 9223372036854.368547758,
     real: 768.12317,
     doubleprecision: 30982137.91023792,
 }

There may be a reason for the decimal and numeric types to return as strings (I am thinking maybe precision?), but I see no reason bigint would be a string.

Note: the BIGSERIAL type also returns as a string.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions