Map Schema

Reviving maps

To revive a map with string keys and any kind of data as values, the map schema can be used, to describe the type of the value expected in the map.

The map schema is defined as

interface RevivalMapSchema<T> {
  map: RevivalSchema<T>
}

Basically, we need to specify a mapproperty which holds the schema information for all map values.

Here's an example of how to use it

class Prop {
  value = 0
  getValue() { return this.value }
}
class Person {
  name = ''
  props: {[key: string]: Prop} = {}
  getName() { return this.name }
  getProps() { return this.props }
  static getRevivalSchema(): RevivalSchema<Person> {
    return {
      type: Person,
      properties: {
        props: {
          map: Prop
        }
      }
    }
  }
}
const data= '{"name": "John", "props": {"foo": {"value": 1}, "bar": {"value": 2}}}'
const p = revive(data, Person)

In the example above, props is a map with values of type Prop.

Last updated

Was this helpful?