there is incoming string data which is date time format,
Its mapping this object fine;
import { types } from 'mobx-state-tree'
export const UserCard = types.model('UserCard').props({
id: types.number,
awarded_at: types.maybeNull(types.string),
})
at path "/0/awarded_at" value "2022-01-25T21:07:30.473502+00:00"
is not assignable to type: (Date | null)
but since its datetime trying this model just changed string to date;
export const UserCard = types.model('UserCard').props({
id: types.number,
awarded_at: types.maybeNull(types.Date),
})
then get this error in setting object;
at path "/0/awarded_at" value
"2022-01-25T21:07:30.473502+00:00"
is not assignable to type:(Date | null)
So how to map incoming string data to Date object
One common technique is to model it as a string like you have done in your first snippet, create a view that creates a
Date
out of it, and use that instead:If you don't like the approach above you can create a
custom
type: