yield
ing a promise loses type information
#1931
Answered
by
BATCOH
davidatsurge
asked this question in
Q&A
-
IIUC, the recommended way to go about async actions in MST is to use generators. But the approach described in the docs loses type safety, right? Consider: import axios from "axios";
type Project = {id: string}
function getProjects() {
return axios.get<Project[]>("https://.example.com/api/projects")
}
function* generator() {
const projects = yield await getProjects(); // HERE: the type of `projects` is `any`!
} Is a manual cast required here? Or is there a way to keep type safety? |
Beta Was this translation helpful? Give feedback.
Answered by
BATCOH
Jul 15, 2022
Replies: 1 comment 1 reply
-
import { toGenerator } from "mobx-state-tree";
...
function* generator() {
const projects = yield* toGenerator(getProjects());
} |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
davidatsurge
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
toGenerator