Types of an types.model narrowed from a union with a literal don't have actions?
#1975
Answered
by
davidatsurge
davidatsurge
asked this question in
Q&A
|
Consider: const Bed = types.model({}).actions((self) => ({
addBlankets() {
1 + 1;
},
}));
const Room = types
.model({
bed: types.union(types.literal("loading"), Bed),
})
.actions((self) => ({
addBlanketsOnBed() {
if (self.bed != "loading") {
self.bed.addBlankets(); // typescript complains that `addBlankets` is not a function here.
}
},
}));I'm currently resorting to: doing |
Answered by
davidatsurge
Oct 20, 2022
Replies: 2 comments 3 replies
|
Sounds like you are mixing state of the application i.e. |
3 replies
|
So, it turns out the following works (ie, doesn't trip up typescript), and it fulfills my desire to "make illegal states unrepresentable", and it follows the more standard pattern of "tagged unions" anyways: const Bed = types.model({}).actions((self) => ({
addBlankets() {
// adding blankets
},
}));
const Room = types
.model({
bed: types.union(
types.model({status: types.literal("loading")}),
types.model({status: types.literal("loaded"), item: Bed})
)
})
.actions((self) => ({
addBlanketsOnBed() {
if (self.bed.status != "loading") {
self.bed.item.addBlankets();
}
},
})); |
0 replies
Answer selected by
davidatsurge
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So, it turns out the following works (ie, doesn't trip up typescript), and it fulfills my desire to "make illegal states unrepresentable", and it follows the more standard pattern of "tagged unions" anyways: