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

Query builder and `ReturnType<T>` causing TypeScript 4.7.4 to hang #421

Open idreyn opened 2 years ago

idreyn commented 2 years ago

See here for a branch of my repo pared down to a minimal reproduction of this issue.

I have a simple function that returns an EdgeQL select expression:

export const getArchiveQuery = (id: string) => {
    return e.select(e.GtfsArchive, (archive) => {
        return { filter: e.op(archive.id, "=", e.uuid(id)) };
    });
};

I want to extract the return type of this function:

type ArchiveQuery = ReturnType<typeof getArchiveQuery>;

This completely breaks TypeScript. It renders the language server unresponsive in VSCode, and causes npx tsc to hang indefinitely. Interestingly, ReturnType<T> looks like a key part of the issue. If I write:

const query = e.select(e.GtfsArchive, (archive) => {
    return { filter: e.op(archive.id, "=", e.uuid("not-a-real-uuid-but-whatever")) };
});

type ArchiveQuery = typeof query;

then TS has no problem extracting that type for me.

I am using:

Ultimately this is a problem with TypeScript, not EdgeDB, and I may file an issue over there too. But you might want to be aware of one way in which the query builder is currently putting TS under too much stress.

fknop commented 2 years ago

Hello, I'm also seeing typescript hanging, however, I'm not using ReturnType.

Here is the kind of query that makes TS hangs for me:

e.select(
    e.group(e.Translation, (translation) => ({
      by: {
        localeId: translation.locale.id,
        translated: e.op('exists', translation.content),
      },
    })),
    (group) => ({
      translated: group.key.translated,
      localeId: group.key.localeId,
      count: e.count(group.elements),
    })
  )

It seems to be the e.group nested inside the e.select as e.group alone doesn't seem to hang.

I'm using: Typescript 4.8.2 edgedb 0.22.2 node 16.13.1

alexgorbatchev commented 1 year ago

I noticed even the most basic schema causes VSCode intellisense to become very slow, i'm talking 30s for types to show up in VSCode. Removing generated TS code from the project restores intellisense performance back to near instant.

scotttrinh commented 1 year ago

The TypeScript team has been making great strides lately on the performance of the type checker. I'd love to hear from anyone who is able to update to the latest TypeScript version who still has LSP issues so we can better determine which bits of our generated code are the worst offenders given the current advances in the type checker.

Admittedly I use a pretty beefy laptop and I'm able to use the latest version of TypeScript (v5.0.4 as of today), and haven't noticed any extreme slowdowns with either vscode or emacs (both using LSP).

Unfortunately, we're not going to be able to help much with older versions of TypeScript directly, but any enhancements we make for type checking performance on the newer versions might have some benefit if you're stuck on older versions 🤞

I'll leave this issue open for a bit to gather any feedback from those who are following it on their latest experiences with TypeScript since the performance improvements in 5.0.