I'm fairly certain I'm using relatively new versions of sqlite3 (5.0.11) and @types/sqlite3 (3.1.8) but the types are not matching the behavior.
Roughly, it seems as though despite @types/sqlite3/index.d.ts
including this declaration:
run(sql: string, params: any, callback?: (this: RunResult, err: Error | null) => void): this;
err
is the first parameter.
What's going on here?
Details:
const db: sqlite3.Database;
this.db.run('DELETE from ... (actually it's invalid SQL)', (result: RunResult, err: Error) => {...});
This is what result
contains:
Error: SQLITE_ERROR: no such table: ANY {errno: 1, code: 'SQLITE_ERROR', stack: 'Error: SQLITE_ERROR: no such table: ANY', message: 'SQLITE_ERROR: no such table: ANY'}
code: 'SQLITE_ERROR'
errno: 1
message: 'SQLITE_ERROR: no such table: ANY'
stack: 'Error: SQLITE_ERROR: no such table: ANY'
I don't know how Error
is really supposed to look, but this looks more like type Error
than type RunResult
.
This is how I invoke the database:
if (params !== undefined) {
this.db.run(sql, params, cb);
} else {
this.db.run(sql, cb);
}
And this is cb
:
const cb = (result: RunResult, err: Error | null) => {
if (err) {
reject(err);
} else {
resolve(result);
}
};
Am I doing something obviously wrong?
Declaring this
in a function tells typescript the type of this
in the function body, it does not get added to the function parameters.
function cb(this: RunResult, err: Error | null) {
if (err) {
reject(err);
} else {
resolve(this); // `this` will be the RunResult
}
}
The above function is still a function will a single parameter err
, we have just told typescript the type of this
in the function body. This does not work for arrow functions