Types of an types.model
narrowed from a union with a literal don't have actions?
#1975
-
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 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Sounds like you are mixing state of the application i.e. |
Beta Was this translation helpful? Give feedback.
-
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();
}
},
})); |
Beta Was this translation helpful? Give feedback.
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: