The recommended way to do asynchronous actions in MobX-state-tree (MST) is to use flow
, which takes a generator function as first argument in which every promise should be yielded.
yield
expressions are of type any
in TypeScript, but is there any way to automatically type a yield expression in MST?
Example
import { flow, types } from "mobx-state-tree"; type Stuff = { id: string; name: string }; function fetchStuff(): Promise<Stuff[]> { return new Promise((resolve) => { resolve([ { id: "1", name: "foo" }, { id: "2", name: "bar" } ]); }); } const Thing = types.model({ id: types.identifier, name: types.string }); const ThingStore = types .model({ things: types.array(Thing) }) .actions((self) => ({ fetchThings: flow(function* () { // "stuff" is of type "any"! const stuff = yield fetchStuff(); self.things.replace(stuff); }) }));
from Recent Questions - Stack Overflow https://stackoverflow.com/questions/65374057/typed-yield-expression-in-mobx-state-tree-flow Tholle
没有评论:
发表评论