Replies: 4 comments
-
Almost. Use See #1393 also. @sidorares Would it be an idea to introduce specific return types for |
Beta Was this translation helpful? Give feedback.
-
This is what I'm using currently. It's an augmentation of the existing module. It only changes the query methods, not the execute ones, but you can do something similar with the execute methods. The benefit of these types is that it can infer the import {
RowDataPacket,
FieldPacket,
QueryOptions,
} from "mysql2/promise";
declare module "mysql2/promise" {
interface QueryOptionsRowsAsArray extends QueryOptions {
rowsAsArray: true;
}
interface Pool extends NodeJS.EventEmitter {
query<T extends RowDataPacket[]>(sql: string): Promise<[T, FieldPacket[]]>;
query<T extends RowDataPacket[]>(
sql: string,
values: any | any[] | { [param: string]: any }
): Promise<[T, FieldPacket[]]>;
query<T extends QueryOptions>(
options: T
): Promise<[T extends infer P ? P extends QueryOptionsRowsAsArray ? RowDataPacket[][] : RowDataPacket[] : never, FieldPacket[]]>;
query<T extends QueryOptions>(
options: T,
values: any | any[] | { [param: string]: any }
): Promise<[T extends infer P ? P extends QueryOptionsRowsAsArray ? RowDataPacket[][] : RowDataPacket[] : never, FieldPacket[]]>;
}
} |
Beta Was this translation helpful? Give feedback.
-
@haroldiedema And how can you be more strict about typing the query result? Let's say you have some |
Beta Was this translation helpful? Give feedback.
-
@raisen I think you can use the 'in' operator to check const [results] = await db.execute('')
if ("length" in results) {
console.log(result.length)
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I read #1329 but still cannot find a way to run queries without using OkPacket[] as type argument to avoid the warning below:
Should I create an interface that automatically uses OkPacket[] as type argument for all the
execute
andquery
calls?Beta Was this translation helpful? Give feedback.
All reactions