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

Type issue after updating to v8.4.0 #797

Closed
hasithaweerasinghe opened this issue Aug 30, 2023 · 3 comments
Closed

Type issue after updating to v8.4.0 #797

hasithaweerasinghe opened this issue Aug 30, 2023 · 3 comments

Comments

@hasithaweerasinghe
Copy link

hasithaweerasinghe commented Aug 30, 2023

Hey everyone,

I hope you're doing well. I've recently encountered a type-related issue after updating ArangoJS from version 8.0.0 to 8.4.0 in our project. This issue pertains to the changes in type definitions that seem to have caused a regression in some core functionalities.

export async function getUser(name:string) {

  const results = await db.query(
      aql`FOR u in User FILTER ${name} == u.name RETURN u`
  );
  
  return await results.next();

}

The return type of the above database query has been updated to (8.4.0) Promise<ArrayCursor<undefined>>, whereas in version 8.0.0, it was Promise<ArrayCursor<any>>. Although it is possible to specify a type for the AQL string handler, it appears that the default type of any is no longer functioning as expected.

export declare function aql<T = any>(templateStrings: TemplateStringsArray, ...args: AqlValue[]): GeneratedAqlQuery<T>;

Following image shows the VScode intellisense types:

image

Thanks in advance!

@pluma4345
Copy link
Member

pluma4345 commented Sep 13, 2023

This feels like it's a bug in TypeScript actually. You can get the correct behavior by splitting the code into two statements:

const query = aql`FOR u in User FILTER ${name} == u.name RETURN u`; // type: GeneratedAqlQuery<any>
const results = await db.query(query); // type: ArrayCursor<any>

I'm investigating what is triggering this unexpected behavior in TypeScript.

@pluma4345
Copy link
Member

With the help of @nikhil-varma we found multiple ways to force the intended behavior:

  1. Removing the GeneratedAqlQuery type so aql casts its return value to AqlQuery instead.
  2. Adding an overload to query that explicitly accepts a GeneratedAqlQuery argument.
  3. Changing the type marker from [type]?: T to [type]: T.
  4. Changing the type marker from [type]?: T to [type]?: T | any.

We went with the fourth solution as it forces the intended behavior without requiring major changes to the code or impacting backwards compatibility.

@djanogly
Copy link

Thank you @pluma4345 & @nikhil-varma - @hasithaweerasinghe is off today and back Monday, so will try these fixes then and report back.

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

3 participants