gcanti / babel-plugin-tcomb

Babel plugin for static and runtime type checking using Flow and tcomb
MIT License
482 stars 22 forks source link

Checking return value of generator function breaks the code #162

Closed danielkcz closed 7 years ago

danielkcz commented 7 years ago

I am using generators for my async flow rather then await/async. I understand type checking can be tricky there, but consider this simple use case

function* fetchCharge(price: number): Charge {
    const user: User = yield this.fetchCurrentUser()
    return this.createCharge({ user, price })
}

It gets transpiled like this

function* fetchCharge(price) {
    _assert(price, _tcomb2.default.Number, "price");

    const ret = function (price) {
        const user = _assert((yield this.fetchCurrentUser()), User, "user");
        return this.createCharge({ user, price });
    }.call(this, price);

    _assert(ret, Charge, "return value");

    return ret;
}

As you can see main issue is that wrapping function is not a generator so things will fail with yield keyword in there. In should probably look like this...

const ret = function* (price) { .... }.call(this, price);
_assert(yield ret, Charge, "return value");
danielkcz commented 7 years ago

Um, bump? :)

This got even worst because I started using Flow as well and in same cases it requires me to supply annotation. And of course if I do that, the generated code gets broken because of this plugin.

danielkcz commented 7 years ago

For anyone interested, the flow-runtime module has this solved and can replace tcomb completely.