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

Unexpected type inference on array fields #121

Closed
Eprince-hub opened this issue Apr 11, 2023 · 2 comments
Closed

Unexpected type inference on array fields #121

Eprince-hub opened this issue Apr 11, 2023 · 2 comments

Comments

@Eprince-hub
Copy link

Describe the bug
SafeQL infers any type on fields that returns an array, leading to a Query has incorrect type annotation error when type any is not passed to this field.
The example below will throw the above error on FoodNames because a type other than any is being passed

type AnimalWithFoodName = {
  firstName: string;
  foodNames: string[];
};

export async function queryJsonAgg() {
  return await sql<AnimalWithFoodName[]>`
    SELECT
       first_name,
       (SELECT json_agg(name) FROM foods) AS food_names
    FROM
      animals
  `;
}

To Reproduce
Steps to reproduce the behavior:

  1. Setup SafeQL
  2. Use this code in .ts file
type AnimalWithFoodName = {
  firstName: string;
  foodNames: string[];
};

export async function queryJsonAgg() {
  return await sql<AnimalWithFoodName[]>`
    SELECT
       first_name,
       (SELECT json_agg(name) FROM foods) AS food_names
    FROM
      animals
  `;
}

Expected behavior
A clear and concise description of what you expected to happen.
I expect that SafeQL doesn't throw an error when the correct type is being passed and should infer the correct type
Using type any is not encouraged in TypeScript and should also be discouraged in SafeQL

Screenshots
If applicable, add screenshots to help explain your problem.
Incorrect type annotation error when the correct type is passed
Screenshot 2023-04-11 at 10 08 05

No error when any type is passed
Screenshot 2023-04-11 at 10 09 20

Desktop (please complete the following information):

  • OS: MAC OS
  • PostgreSQL version 15
  • Version [e.g. 22]

Additional context
Add any other context about the problem here.

@Newbie012
Copy link
Collaborator

json_agg method returns a json, which by default I map to any since the majority of the Typescript community prefer any over unknown. But you could change that behavior in the overrides.types.

What you want is array_agg instead:

type AnimalWithFoodName = {
  firstName: string;
  foodNames: string[];
};

export async function queryJsonAgg() {
  return await sql<AnimalWithFoodName[]>`
    SELECT
       first_name,
       (SELECT array_agg(name) FROM foods) AS food_names
    FROM
      animals
  `;
}

@Eprince-hub
Copy link
Author

Thank you, @Newbie012, I will give this a try

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

2 participants