Types for get()
method of a reference?
#1930
Answered
by
EmilTholin
davidatsurge
asked this question in
Q&A
-
const RootStore = types.model({
inbox: InboxStore,
activeProject: types.maybeNull(
types.reference(ProjectInbox, {
get(identifier: string, parent /* HERE: What type do I use for `parent`? */) {
return parent.inbox.projects.find(({ id }) => id == identifier) || null;
},
set(project) {
return project.id;
},
})
),
}); If I set the type of PS: I am absolutely excited to have discovered this library. |
Beta Was this translation helpful? Give feedback.
Answered by
EmilTholin
Jul 14, 2022
Replies: 1 comment 1 reply
-
Hi @davidatsurge! As you figured out TS sadly cannot figure out how to type Example const RootStore = types.model({
inbox: InboxStore,
activeProject: types.maybeNull(
types.reference(ProjectInbox, {
get(identifier, parent) {
if (parent === null) return null;
const inbox = parent.inbox as Instance<typeof InboxStore>;
return (
inbox.projects.find(({ id }) => id === identifier) || (null as any)
);
},
set(project) {
return project.id;
}
})
)
}); |
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
Hi @davidatsurge!
As you figured out TS sadly cannot figure out how to type
RootStore
ifRootStore
is used in its own definiton, so what you could do is type theparent.inbox
toInstance<typeof InboxStore>
instead to get around this.Example