JQuery Promises Chain + Typescript = Type Mismatch

I'm having difficulty with TS trying to associate jQuery Promises with .then

See my code below:

function first(): JQueryPromise<string>
{
    return $.when('1');
}

function test()
{
    $.when()
        .then(() =>
        {
            return first();
        })
        .then((value) =>
        {
            var str: string = value; //<--- type mismatch here.
        });
}

Typescript expects this value to be of type JQueryPromise instead of "string".

If I pass the value to anyone, I can make it work.

Is there any other way to implement it, or is there an error with the jQuery definition file?

thank

+4
source share
1 answer

JQuery promises has a complex structure in a specific definition file, because jQuery promises themselves have a complex structure / history. You should help the compiler choose the right overload by providing a general argument, rather than relying on the output:

.then<string>(() =>
{
    return first();
})

, promises, , Q.js. Q Promises/A +, ES6 promises, , inferencing TypeScript, , Q.all.

+1

All Articles