drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
21.52k stars 487 forks source link

[BUG]: Losing kysely types (using Kyselify) - getting [x: string]: any #2428

Open trompx opened 1 month ago

trompx commented 1 month ago

What version of drizzle-orm are you using?

0.31.0

What version of drizzle-kit are you using?

0.20.13

Describe the Bug

Hello.

I have some types problems using kysely with drizzle. I have the following config:

import { CamelCasePlugin, Kysely, PostgresDialect } from "kysely";
import { Kyselify } from "drizzle-orm/kysely";
import { createPool } from "#connectors/create-pool";
import {
  posts,
} from "#schema";

export interface KyselyDatabase {
  posts: Kyselify<typeof posts>;
}

export const db = new Kysely<KyselyDatabase>({
  dialect: new PostgresDialect({
    pool: createPool({
      max: 1,
    }),
  }),
  plugins: [new CamelCasePlugin()],
});

When I do the following query:

export const getPosts = async () => {
  const posts = await db
    .selectFrom("posts")
    .select(["title", "id"])
    .execute();
  return posts ?? null;
};

In vscode, I get the following type:

const getPosts: () => Promise<{
  [x: string]: any;
}[]>

It doesn't infer the title and id types automatically so I have a bunch of type error in my code when using posts returned value. Am I doing something wrong or there is a problem with Kyselify?

Expected behavior

I should get the following type:

const getPosts: () => Promise<{
  title: string;
  id: string;
}[]>

Environment & setup

Vscode - Linux

septatrix commented 1 month ago

2409 might also be of interest to you. It's about adding a type-safe, logically ordered API natively to drizzle