kysely: `Kysely` type errors with narrow table name types

PSA:

Read the 0.28.2 release before commenting here. Your issue might not be relevant to this issue, at all.


Since 0.28.0:

import { Kysely } from "kysely";

async function foo(table: "moshe" | "ehsom", column: string) {
  await (db as Kysely<any>)
    .selectFrom(table)
    .select(column) // ❌ errors although we cast to Kysely<any>
    .execute();

  await (db as Kysely<any>)
    .selectFrom(table as string) // ✅ does not error although it is even broader
    .select(column)
    .execute();
}

See playground.

Also see #1377.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 22 (18 by maintainers)

Commits related to this issue

Most upvoted comments

@waynesbrain oh no, please keep using Kysely. We need entitled assholes like you to say more mean things to us!

I think we need to spend time writing much more benchmarks if we want to be able to draw conclusions from them.

What am I missing?

Nothing, sorry. For some reason I thought he used Kysely<any> in that helper.

👋 I encountered this today, but wanted to thank you for the discussion here, and the effort put into this wonderful library. It’s encouraging to see the bug being taking seriously, while at the same time balancing the need to maintain and improve performance.

As an example where I found the previous behavior helpful: I have some generic helper functions to avoid rewriting common queries for every table. These tables have some uniformity in columns, so there are functions similar to this:

function deleteByIdQuery(id: string, db: Kysely<DB>, table: keyof DB) {
  return db.deleteFrom(table).where('id', '=', id);
}

The idea behind these is testing the price in instantiations of a single method’s addition to an existing chain. The upstream should be cached as baseline to have zero price - in reality I believe the compiler caches selectFrom results (and their subresults). Instantiations give a stable score that often translates to less work thus less time, while time benchmarks are usually all over the place and change between machines/environments so it’s very hard to tell whether you’re making progress or which hypothesis is better.

I’m not arguing with this. But let’s say we have this query:

db.selectFrom('person')
  .select(['id', 'first_name'])
  .where('age', '>', 10)
  .execute()

Now let’s say selectFrom creates 400 instantiations, select creates 2000, where creates 500 and execute 10. My change made selectFrom create 20 instantations more. That’s 20 / 2910 == 0.7% of the total.

Those numbers are made up since we have no data. That 5% slower means very little when we don’t know the whole story.

On the other hand, we fixed a weird bug. If you have a Kysely<any> instance, you should be able to give it anything. Needing to downcast everything to string is no better than needing to downcast to any.

Found an easy fix and it did this to our typescript benchmark:

Image

So the successful (super fast) cases got 5% slower but the failure cases got 20% faster. @igalklebanov do you think the fix is worth it? By the way, the fix uses the [TE] extends [keyof DB] trick I just learnt from you 😄