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

knex.table({ t: knex.select(...) }) style is supported but error by type check #6035

Open
LumaKernel opened this issue Mar 13, 2024 · 0 comments

Comments

@LumaKernel
Copy link

Environment

Knex version: 2.4.2 ( and also confirmed with 3.1.0 )
Database + version: pg 14
OS: any

Select applicable template from below.
If issue is about TypeScript definitions, tag @lorefnon.

Bug

  1. Explain what kind of behaviour you are getting and how you think it should do

knex.table({ t: knex.select(...) }) style select is supported by knex runtime, but not accepted by type def of knex.

And renaming sub-queries is necessary for postgres.

  1. Error message
No overload matches this call.
  The last overload gave the following error.
    Object literal may only specify known properties, and 't' does not exist in type 'Raw<any>'. ts(2769)
index.d.ts(1387, 5): The last overload is declared here.
(property) t: Knex.QueryBuilder<{}, (DeferredKeySelection<{}, string, true, {
    count: Knex.Raw<any>;
}, boolean, {}, unknown> | DeferredKeySelection<{}, never, true, {
    count: Knex.Raw<any>;
}, false, {}, never>)[]>
image
  1. Reduced test code, for example in https://npm.runkit.com/knex or if it needs real
    database connection to MySQL or PostgreSQL, then single file example which initializes
    needed data and demonstrates the problem.

RunKit can't show the type def, so please see this codespace example

import { knex } from "knex";

const fakeMain = async () => {
  const db = knex({});
  await db.table({
    // for nested query, pg requires to alias sub queries.
    t: db.select({
      a: 1,
      c: db.raw('COUNT(*)'),
    }),
  }).select({
    a: 'a',
    c: 'c',
    desc: db.raw('?', ['this is my query']),
  });
};

As a workaround, we can put this code to somewhere module-mode ts file to declare this use-case.

declare module 'knex' {
  // eslint-disable-next-line @typescript-eslint/no-namespace
  namespace Knex {
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    interface Table<TRecord extends {} = any, TResult = any> {
      <
        TRecord2 extends {} = {},
        // TResult2 = DeferredKeySelection.ReplaceBase<TResult, TRecord2>, // omitted for easy
        TResult2 = any
      >(
        raw: Record<string, Knex.TableDescriptor>,
        // options?: TableOptions, // omitted for easy
        options?: never,
      ): QueryBuilder<TRecord2, TResult2>;
    }
  }
}
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