javascriptreactjsmobxmobx-state-tree

How to have a nullable string value in Mobx-State-Tree


I'm trying to create a model with a optional, nullable string value. I've tried using both

  hint: types.optional(types.string, ""),

and

  hint: types.maybe(types.string),

Both results in error when I try to set a json object as to the object. Works if I manually loop through the json object and set the null content to empty string "".

Error while converting "jsoncontent" at path "content" value null is not assignable to type: string (Value is not a string).


Solution

  • You can use types.maybeNull to have a type that can also be null.

    hint: types.maybeNull(types.string)