-
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
Self reference model with Typescript and MST v3 #943
Comments
The only ugly way I was able to do this is there:
|
Another aproach is to manualy resolve references. Still waiting for comments.
|
Could you create a TS based code sandbox so that others can chime in?
Op di 24 jul. 2018 09:44 schreef AndrewSorokin <notifications@github.com>:
… Another aproach is to manualy resolve references.
Still waiting for comments.
import {
types, getRoot, resolveIdentifier
} from 'mobx-state-tree';
export const Model = types.model(
{
key: types.identifier,
num: types.number,
refKey: types.string
})
.views(self => ({
get ref() {
const root = getRoot(self);
const ref = resolveIdentifier(Model , root, self.refKey);
return ref as typeof Model.Type;
}
}))
.actions(self => ({
method: (t: number) => {
return t.toString();
}
}))
export const m = Model.create({
key: '1',
num: 0,
refKey: '1'
});
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#943 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABvGhL5o3OyaQ52lkvEcjfxsBV8nQsM5ks5uJtBJgaJpZM4Vagl_>
.
|
Sure, here is it. Example with manual reference resolution I don't like that because of redundancy and some extra work I need to do. Example with explicit typings: It looks a bit ugly. |
There seems to be no clean way to fix, as TS really can't handle the type of something self referring. Even upcasting doesn't work. However, using a temporary variable seems to work around it: let tmp: any
/// BrokenTypingsModel has any type or leads to TS compiler error
const BrokenTypingsModel = types.model({
key: types.identifier,
brokenRef: types.maybe(types.reference(types.late<BrokenTypeLike, BrokenTypeLike, BrokenTypeLike>(() => tmp)))
})
interface BrokenTypeLike {
key: string
brokenRef: BrokenTypeLike
}
tmp = BrokenTypingsModel
const b = BrokenTypingsModel.create({ key: '3' })
console.log(b.brokenRef ? b.brokenRef.key : 'nothing') |
Is the problem described here similar to the one described in this issue? |
@bourquep no that is a different problem, the issue here is like expressing a self-referring structure |
Closing as a can't fix at this moment :-( |
actually using 'this' for actions defined in the same scope is also possible (like for views), maybe the readme should be updated with that? |
Hmm yes that might be outdated indeed
Op ma 27 aug. 2018 om 19:47 schreef Javier Gonzalez <
notifications@github.com>:
… actually using 'this' for actions defined in the same scope is also
possible (like for views), maybe the readme should be updated with that?
—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
<#943 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABvGhHkJY0gpYYr25zbXKCQnUa4p9SApks5uVDDLgaJpZM4Vagl_>
.
|
I'm having trouble getting the workarounds using That said, there seems to be a couple of new solutions for circular references using conditional types. See This Comment and this Comment in the TypeScript repo. Would either of these be applicable here? |
I can confirm
|
I'm not sure if you are using late to fix cicular/self-references, but if so, from the docs: If you are using TypeScript and you get errors about circular or self-referencing types then you can partially fix it by doing:
In this case, while "me" will become any, any other properties (such as x) will be strongly typed, so you can typecast the self referencing properties (me in this case) once more to get typings. For example:
|
thanks @xaviergonz I hoped there will be a better way. I end up using views
|
Nice trick! Never thought of adding a view afterwards to actually make its type proper again! |
Worth adding this trick to TS FAQ |
@elie222 PR welcome! |
Sure. Just had a look here and I see the https://mobx-state-tree.gitbook.io/docs/faq Where is the repo for these docs? |
Sorry, will just update the official README |
Ah. I see it's already there :) https://github.com/mobxjs/mobx-state-tree/blob/master/docs/API/README.md#late |
some possible solution: var User = struct({
user: () => User,
});
User.user.user.user // ok
function struct<T>(obj: T) {
return obj as {[P in keyof T]: T[P] extends () => infer R ? R : T[P]};
} |
Implements the trick mentioned in this comment: mobxjs/mobx-state-tree#943 (comment) Every late() reference field will be of type IAnyModelType and will be accompanied by a view getter casting it to the actual type of the prop. After this a reference field name "refField" should be accessed as "refFieldProp" which will provide correct typings/autocomplete in IDEs. This trick works up to typescript 3.4.x and doesn't work in 3.5 and above :-( Using Instance<...> like this however still works in all versions: mobxjs/mobx-state-tree#943 (comment) refs mobxjs#45
@kresli Isn't it results in the same error about the self-referenced variable? It's still part of the same declaration, so TS will complain that it can't infer its type, even despite it's in a views clause |
@ArmorDarks No it shouldn't because the model part would complain about circulation. So in the getter you would type model where |
Please don't reply on closed issues |
I have some issue with explicit model typing in v3.
I need to get model with self reference with typescript.
There is solution #417 for MST v2 smth. like that:
What is a proper way to express model type with MST v3?
The text was updated successfully, but these errors were encountered: