Skip to content

Type, that fallbacks null | undefined to [] #1868

Answered by EmilTholin
haalogen asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @haalogen!

types.optional has a third optional argument optionalValues that you can use to specify what values you want to override with the defaultValue.

Example

import { types } from "mobx-state-tree";

const Type = types.array(types.string);

const OtherType = types.model({
  type: types.optional(Type, [], [undefined, null])
});

const otherType = OtherType.create({
  type: null
});

console.log(otherType.type); // []

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by EmilTholin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants