Mobx-state-tree: Error: [mobx-state-tree] expected a mobx-state-tree type as first argument, got function array

Created on 20 Mar 2018  Â·  10Comments  Â·  Source: mobxjs/mobx-state-tree

Works:

types.model({
  food: types.optional(types.string, ``),
})

Throws:

types.model({
  food: types.optional(types.array, []),
})

why?

Most helpful comment

In that case use either a union of the things you know it can be, or
frozen. It is kinda the any type of MST, which this understanding, you
will not going to mutate the values (replacing is fine), as MST wont reason
about frozen things, but treat them as-is verbatim values

Op wo 21 mrt. 2018 om 00:32 schreef Cory Simmons notifications@github.com:

Let's say I don't know what type might show up in that array (e.g. [1,
'str', { a: 2 }]), is there a types.any I'm missing?

looks like i should go to bed instead of coding :D

lol, I feel ya. Get some zzz

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/mobxjs/mobx-state-tree/issues/717#issuecomment-374792653,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABvGhNGFz_6Uo6eZBoa0Uz5H9kZ116u7ks5tgZF1gaJpZM4Sydhq
.

All 10 comments

Also, how do I make food an empty array by default and load it with fetch'd JSON? I know how to use flow and await async data, but is there a preferred way to approach this common task?

could you please provide a codesandbox to reproduce it?
the code you posted looks good.

@robinfehr Sure, https://codesandbox.io/s/l4jp1nnjj9

Just uncomment lines in both ShoppingList files to see err thrown.

Does types.array _always_ need to pass a subtype like types.array(types.string)?

huch - looks like i should go to bed instead of coding :D

it needs to be:
types.optional(types.array(types.string), [])

or some subtype instead of string.

Let's say I don't know what type might show up in that array (e.g. [1, 'str', { a: 2 }]), is there a types.any I'm missing?

looks like i should go to bed instead of coding :D

lol, I feel ya. Get some zzz

In that case use either a union of the things you know it can be, or
frozen. It is kinda the any type of MST, which this understanding, you
will not going to mutate the values (replacing is fine), as MST wont reason
about frozen things, but treat them as-is verbatim values

Op wo 21 mrt. 2018 om 00:32 schreef Cory Simmons notifications@github.com:

Let's say I don't know what type might show up in that array (e.g. [1,
'str', { a: 2 }]), is there a types.any I'm missing?

looks like i should go to bed instead of coding :D

lol, I feel ya. Get some zzz

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/mobxjs/mobx-state-tree/issues/717#issuecomment-374792653,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABvGhNGFz_6Uo6eZBoa0Uz5H9kZ116u7ks5tgZF1gaJpZM4Sydhq
.

@mweststrate thanks!

So what was the solution? I am having the same issue right now.

any update?

Was this page helpful?
0 / 5 - 0 ratings