haxe-js-kit icon indicating copy to clipboard operation
haxe-js-kit copied to clipboard

Bluebird.hx type error chaining * -> Void functions with .then()

Open laurence-myers opened this issue 10 years ago • 2 comments

I'm having trouble chaining functions/promises using the Bluebird extern. I believe Haxe's type inference engine is failing to deduce the return type of a fulfilledHandler that comes after a fulfilledHandler with a return type of Void.

Here's a small test case that exhibits the problem:

import buddy.BuddySuite;
import js.npm.Bluebird;
using buddy.Should;

class TestBluebird extends BuddySuite {
    public function new() {

        describe("Testing Bluebird", function () {
            it("Allows chaining after a '* -> Void' function", function (done) {
                var p = new Bluebird(function (resolve, reject) {
                    resolve("first value");
                }).then(function (firstValue) {
                    firstValue.should.be("first value");
                }).then(function () {
                    done();
                });
            });
        });
    }
}

I think I can work around the issue by returning "null" in one function and accepting a dummy arg in the next, but this is awkward for chaining existing functions together, and problematic for making use of externs or libs.

laurence-myers avatar Oct 26 '15 00:10 laurence-myers

Can you please make a PR with your test in https://github.com/clemos/haxe-js-kit/tree/master/test/buddy/src ? It's indeed always difficult to keep JS's flexibility while typing stuff as much as possible, especially with method signatures / optional arguments. Regarding the extern, you try to change T -> Tx signatures to ?T -> Tx (or ?T -> ?Tx ?); otherwise, you'll probably need to add more @:overload with Void -> Tx and/or Void -> Void ?

clemos avatar Oct 26 '15 08:10 clemos

Yes, I'm finding it's quite difficult. If I use something simple like the "Promise.hx" stub contained in the Sequelize dir, that makes use of Dynamic, it works pretty well but loses type safety.

I had no idea you could have optional type parameters! That's amazing! :smile:

I'm still learning Haxe, so apologies if I'm just doing something silly and not understanding how to enforce the types.

laurence-myers avatar Oct 26 '15 10:10 laurence-myers