Skip to content

Interpolated booleans inferred as boolean | null (nullability) #268

@karlhorky

Description

@karlhorky

Describe the bug

TypeScript boolean types in template string interpolation are not correctly inferred as boolean, but instead as boolean | null

To Reproduce
Steps to reproduce the behavior:

// 💥 Query has incorrect type annotation.
//	Expected: { has_private_url: boolean; }
//	Actual: { has_private_url: boolean | null; }[]
await sql<{ has_private_url: boolean }[]>`
  SELECT
    ${true as boolean} AS has_private_url
  FROM
    users
`;

Also same problem with no TS type assertion:

// 💥 Query has incorrect type annotation.
//	Expected: { has_private_url: boolean; }
//	Actual: { has_private_url: boolean | null; }[]
await sql<{ has_private_url: boolean }[]>`
  SELECT
    ${true} AS has_private_url
  FROM
    users
`;

Expected behavior

Inference as boolean

Screenshots

--

Desktop (please complete the following information):

  • OS: macOS Sequoia 15.0 (24A335)
  • PostgreSQL version 14
  • Version 3.4.4

Additional context

I was thinking that this may relate to the isColumnNonNullable() function, but since I cannot see anything in the AST related to interpolated values (also not sure what keywords to search for), I'm thinking that maybe the interpolation happens at an earlier step, before the parsing, eg somewhere related to this code:

const tsTypeToPgTypeMap: Record<string, string> = {
number: "int",
string: "text",
boolean: "boolean",
bigint: "bigint",
any: "text",
unknown: "text",
};
const tsKindToPgTypeMap: Record<number, string> = {
[ts.SyntaxKind.StringLiteral]: "text",
[ts.SyntaxKind.NumericLiteral]: "int",
[ts.SyntaxKind.TrueKeyword]: "boolean",
[ts.SyntaxKind.FalseKeyword]: "boolean",
[ts.SyntaxKind.BigIntLiteral]: "bigint",
};
const tsFlagToTsTypeStringMap: Record<number, string> = {
[ts.TypeFlags.String]: "string",
[ts.TypeFlags.Number]: "number",
[ts.TypeFlags.Boolean]: "boolean",
[ts.TypeFlags.BigInt]: "bigint",
[ts.TypeFlags.NumberLiteral]: "number",
[ts.TypeFlags.StringLiteral]: "string",
[ts.TypeFlags.BooleanLiteral]: "boolean",
[ts.TypeFlags.BigIntLiteral]: "bigint",
};
const tsFlagToPgTypeMap: Record<number, string> = {
[ts.TypeFlags.String]: "text",
[ts.TypeFlags.Number]: "int",
[ts.TypeFlags.Boolean]: "boolean",
[ts.TypeFlags.BigInt]: "bigint",
[ts.TypeFlags.NumberLiteral]: "int",
[ts.TypeFlags.StringLiteral]: "text",
[ts.TypeFlags.BooleanLiteral]: "boolean",
[ts.TypeFlags.BigIntLiteral]: "bigint",
};

With a few hints as to how the system works, I would love to open a PR fixing this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions