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
- fix #1440 — committed to kysely-org/kysely by koskimas 2 years ago
- fix #1440 — committed to kysely-org/kysely by koskimas 2 years ago
- Fix #1440 (#1443) * fix #1440 * Rebaseline type benchmarks — committed to jlucaso1/kysely by koskimas a year ago
@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.
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:
I’m not arguing with this. But let’s say we have this query:
Now let’s say
selectFromcreates 400 instantiations,selectcreates 2000,wherecreates 500 andexecute10. My change madeselectFromcreate 20 instantations more. That’s20 / 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 tostringis no better than needing to downcast toany.Found an easy fix and it did this to our typescript benchmark:
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 😄