edgedb / edgedb-js

The official TypeScript/JS client library and query builder for EdgeDB
https://edgedb.com
Apache License 2.0
508 stars 65 forks source link

Type of client.run() with filter_single incorrectly inferred as array #719

Closed jvassev closed 10 months ago

jvassev commented 1 year ago

Code The code causing the error.


async function test(client, id: string) {
  const myWidget = await e.select(e.default.Widget, () => ({
    filter_single: { id: id },
  })).run(client);

  // myWidget inferred as Array<{id: string}>
  if (!Array.isArray(myWidget)) {
    throw new Error("expected an array");
  }
}

Schema

Your application schema.

module default {
  type Widget {
    name: str;
  }
}

Generated EdgeQL

No errors in edgeql

Error or desired behavior

No errors at compile time. At runtime, I expect an array of objects, however I get either null of a single element. I thinks it better to fix the inferred type to be T | null or wrap the single result in an array.

Is there a workaround for this. Now I'm using const myWidget: any to

Versions (please complete the following information):

scotttrinh commented 1 year ago

Yeah, I noticed that a while back, thanks for reporting. In the meantime, you can work around this by wrapping in e.assert_single(e.assert_exists(yourQuery)). Will fix!

scotttrinh commented 1 year ago

Might be related to #715

sharifzadesina commented 1 year ago

I don't understand this issue, right now if you use filter_single the result is T | null, what is wrong with this?

scotttrinh commented 10 months ago

Oh, sorry, I think I misunderstood the original report: filter_single will indeed give you T | null as @sharifzadesina pointed out, so this is working as intended.