Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flow functions are not type checked by FlowType #1769

Closed
thejuan opened this issue Oct 11, 2018 · 6 comments
Closed

Flow functions are not type checked by FlowType #1769

thejuan opened this issue Oct 11, 2018 · 6 comments

Comments

@thejuan
Copy link

thejuan commented Oct 11, 2018

FlowType sees this inside a flow function as any and so no type checking takes place on this calls.
You can get around it by binding the generator function passed to flow.

flow(function*(){yield 1;}.bind(this))

It's easy to forget todo this and I haven't found a lint for it.
Are there any other suggestions on how to get type checking?
Is there a reason why flow isn't a decorator? Is it dangerous in some way if i added it locally?

@mweststrate
Copy link
Member

The primary problem is that arrow functions cannot be combined with generators, so .bind seems to be a nice work around.

flow can be used as decorator when using asyncAction from mobx-utils (no worries, it is just a wrapper, same implementation): https://github.com/mobxjs/mobx-utils#asyncaction. Bu this works only with TypeScript, as it doesn't work for babel legacy. Babel stage 2 non legacy decorators are under way, so will add it as todo there.

@cncolder
Copy link
Contributor

cncolder commented Jan 9, 2019

for typescript, you can type this by hand:

class MyStore {
    @observable
    value = ''

    print = flow(function*(this: MyStore) {
        // type checking worked.
        console.log(this.value.toLowerCase())
    })
}

@thejuan
Copy link
Author

thejuan commented Jan 11, 2019

You still loose the return type if you have one. We went with a Babel Transform to convert async methods to flow during transpilation.

@joshua1
Copy link

joshua1 commented Feb 9, 2019

@mweststrate can We use mobx-task here for async actions then. would that suffice?

@mweststrate
Copy link
Member

Closing for now, will be reopened once #1928 happens.

@davidgolden
Copy link

It seems to me that functions inside flow are bound to this as an arrow function would be:

const ctx = this
const args = arguments
const runId = ++generatorId
const gen = action(`${name} - runid: ${runId} - init`, generator).apply(
    ctx,
    (args as any) as Args
)

https://github.com/mobxjs/mobx/blob/master/src/api/flow.ts

Am I understanding this wrong?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants