kysely-org / kysely

A type-safe typescript SQL query builder
https://kysely.dev
MIT License
10.71k stars 271 forks source link

MySQL2 createPool return type mismatch #1082

Closed thekiwi closed 3 months ago

thekiwi commented 3 months ago

I'm attempting to use the latest Kysely (0.27.4) with the latest MySQL2 (3.10.3) and I'm getting a type mismatch when assigning createPool() to the pool property.

Here is my simple code (copied from here):

import { createPool } from "mysql2";
import { Kysely, MysqlDialect } from "kysely";

const dialect = new MysqlDialect({
  pool: createPool({
    database: 'test',
    host: 'localhost',
    user: 'admin',
    password: '123',
    port: 3308,
    connectionLimit: 10,
  })
})

And here is the Typescript error:

Type 'Pool' is not assignable to type 'MysqlPool | (() => Promise<MysqlPool>)'.
  Type 'Pool' is not assignable to type 'MysqlPool'.
    Types of property 'getConnection' are incompatible.
      Type '(callback: (err: any, connection: PoolConnection) => any) => void' is not assignable to type '(callback: (error: unknown, connection: MysqlPoolConnection) => void) => void'.
        Types of parameters 'callback' and 'callback' are incompatible.
          Types of parameters 'connection' and 'connection' are incompatible.
            Property 'query' is missing in type 'PoolConnection' but required in type 'MysqlPoolConnection'.ts(2322)
mysql-dialect-config.d.ts(34, 5): 'query' is declared here.
mysql-dialect-config.d.ts(15, 5): The expected type comes from property 'pool' which is declared here on type 'MysqlDialectConfig'

I tried older mysql2 versions and found it worked at 3.4.0 but then started throwing the error at 3.4.1. The changes between the two were made over a year ago, which makes me wonder why no-one else has encountered this yet, hence me creating this issue to check if I've misconfigured something.

I'll continue to dig and report back what I find, but I'm curious whether anyone has stumbled across this and solved it? And if so, perhaps we can get the docs updated?

thekiwi commented 3 months ago

Ok, I've figured it out - it's not a bug, I just needed to install @types/node as a dev dependency. A super easy solution, but I think it could be useful to mention in the Kysely/MySQL docs?

At least now if anyone comes across the same error, there's a GitHub issue they can search :)

koskimas commented 3 months ago

You pretty much ALWAYS need @types/node with node and typescript. It's not something we should need to teach people. That's like mentioning people need a package.json file.