-
Notifications
You must be signed in to change notification settings - Fork 642
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
[TypeScript] Assigning a simple object to a map property #913
Comments
The problem is that when you call const map1 = types.map(type.string);
const map2 = types.map(type.string); Because it checks for equality of the type by reference, the MST type system doesn't allow this, for example: const map1 = types.map(types.string);
const map2 = types.map(types.string);
const Foo = types
.model({
m: types.maybe(map1)
})
.actions(self => ({
initialize() {
self.m = map2.create({
x: "hello",
y: "world"
});
}
}));
new Foo().initialize(); However, if you did this: const map = types.map(types.string);
const Foo = types
.model({
m: types.maybe(map)
})
.actions(self => ({
initialize() {
self.m = map.create({
x: "hello",
y: "world"
});
}
}));
new Foo().initialize(); It would work because the types are compatible in that case. |
Not sure, but ideally if we could do #893 we could fix this in the future |
@mosho1 thanks, that's very helpful... I think this explains another issue I was seeing. This is somewhat unexpected and disappointing, I had assumed because MST types were immutable that they also had value semantics, so that if you build up a type the same way on 2 separate occasions, then values created by them will be compatible. I guess not 😕 |
MST 3 will allow typesafe applySnapshot (But not assigns) Nowdays, applySnapshot takes
|
Out of curiosity, why is assignment from instances of compatible models not allowed? I would think that an |
You can't attach living model (which is his own tree) to another tree. |
@Bnaya ah, I see... that's a different issue than what I was thinking of then. |
You cannot safely assume one type of model with a similar signuature to be compatible with the other? E.g. are these types compatible: Or what if the properties match, but the action / view signatures don't? This is the same problem as why declaring two javascript classes are not So you can't interchange model types in MST, but you can interchange their snapshots, which is usually good enough, and which is what you were doing initially. The reason that that can't pass the type check is because TS cannot distinguish between the 'read' and 'write' type of a property. However, that is something we can't change and is a TS limitation. E.g. the following doesn't work either, as TS will complain that getter and setter must have the same type:
|
@mweststrate all of what you said are valid reasons why you can't simply treat one instance as an instance of another class... my point was this:
What is the downside of attempting to treat an instance of a different class as a snapshot? If it succeeds structurally as a snapshot, that's good enough. |
suppose you have a tree like
the behavior would be inconsistent if the snapshotting was done automatic
hooks triggered: beforeDetach, afterAttach vs.
hooks triggered: beforeDestroy? afterCreate, afterAttach |
@mweststrate I see, thanks for the illustrating examples, that's very helpful. For context, I was trying to make a more type safe alternative to const Foo = (env: Env) => types
.model({ ... })
.views(self => ({ ... }))
.actions(self => ({ ... }));
const foo = Foo(env).create({ ... }); but it didn't work if I needed to construct the model from the |
Yep that should work!
Op vr 13 jul. 2018 18:01 schreef Tom Crockett <[email protected]>:
… @mweststrate <https://github.com/mweststrate> I see, thanks for the
illustrating examples, that's very helpful. For context, I was trying to
make a more type safe alternative to getEnv, by simply making the model a
function of the env:
const Foo = (env: Env) => types
.model({ ... })
.views(self => ({ ... }))
.actions(self => ({ ... }))
const foo = Foo(env).create({ ... });
but it didn't work if I needed to construct the model from the env in
different places. I suppose this could be solved by simply memoizing the
model function, since the env argument will always be the same in a given
run of the app.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#913 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABvGhI3R4-7OmKKnPci4UKzYdlq7JwkUks5uGMRngaJpZM4VISjQ>
.
|
In TypeScript, it doesn't seem easy to assign to a map prop in a type safe way. For example,
To work around it I would've thought I could write
but this causes a runtime error
The text was updated successfully, but these errors were encountered: