Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

like image 953
AbsoluteSith Avatar asked Aug 14 '18 11:08

AbsoluteSith


2 Answers

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

hint: types.maybeNull(types.string)
like image 119
Tholle Avatar answered Sep 26 '22 15:09

Tholle


You can use one of the following solutions to have a nullable string value in a Mobx-State-Tree:

types.maybeNull(types.string) // value can be null

or

types.optional(types.string, '') // should create empty string if value is not defined
like image 43
Petr Lazarev Avatar answered Sep 22 '22 15:09

Petr Lazarev