Home > Net >  Type definition of new Promise with SQLite
Type definition of new Promise with SQLite

Time:10-11

I am new with Typescript and I am having problems with the type definition of a Promise. Also, I am using SQLite for the first time. Typescript advised me to put void for resolve, but at the same time it says that on reject void is not assignable on type boolean. This is the code:

import * as SQLite from 'expo-sqlite';

const db = SQLite.openDatabase('address.db');

export const init = () => {
    return new Promise<void>((resolve, reject) => {
        db.transaction((tx) => {
            tx.executeSql(`CREATE TABLE IF NOT EXISTS user (
                name TEXT NOT NULL,
                email TEXT NOT NULL,
                password TEXT NOT NULL
            )`,
            [],
            () => resolve(),
            (_, err) => reject(err),
            )
        });
    });
}

This is the error on the console: Argument of type '(_: SQLTransaction, err: SQLError) => void' is not assignable to parameter of type 'SQLStatementErrorCallback'. Type 'void' is not assignable to type 'boolean'.

If there is someone that can help me, I'd appreaciate it. Thanks in advance

CodePudding user response:

I don't know if it is the right solution, but it stop complaining after this definition:

(_, err): boolean | any => reject(err),

This is the whole code:

import * as SQLite from 'expo-sqlite';

const db = SQLite.openDatabase('address.db');

export const init = () => {
    return new Promise<void>((resolve, reject) => {
        db.transaction((tx) => {
            tx.executeSql(`CREATE TABLE IF NOT EXISTS user (
                name TEXT NOT NULL,
                email TEXT NOT NULL,
                password TEXT NOT NULL
            )`,
            [],
            () => resolve(),
            (_, err): boolean | any => reject(err),
            )
        });
    });
}
  • Related