Search code examples
node.jspromisees6-promisetypescript2.0

Promise: Argument of type void is not assignable to type T


I have an error as above, but I could't find anything wrong with my code.

write(data: T): Promise<T> {
    return new Promise((resolve, reject) => {
        this.open()
        .then((db) => {
            return db.collection(this.table);
        })
        .then((table) => {
            table.insertOne(data)
        })
        .then((result) => {
            console.log(result);
            resolve(result);
        })
        .catch((err) => {
            reject(err);
        })
    })
}

I tried to put that as

write(data: T): Promise<any>

and it worked like a charm. But wonder, what did I do wrong? Can someone please point me to the correct code?

Thank you


Solution

  • It looks like your promise chain might be breaking here:

    .then((table) => {
        table.insertOne(data)
    })
    

    As you're not returning a value here, result isn't going to have a value in the next then. I expect TypeScript is just warning you about this potential mistake.