Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
karlhorky opened this issue Sep 21, 2024 · 1 comment
Open

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

karlhorky opened this issue Sep 21, 2024 · 1 comment

Comments

@karlhorky
Copy link
Collaborator

karlhorky commented Sep 21, 2024

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.

@karlhorky
Copy link
Collaborator Author

Workaround

Wrap the interpolation in coalesce():

await sql<{ has_private_url: boolean }[]>`
  SELECT
    coalesce(${true as boolean}) AS has_private_url
  FROM
    users
`;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant