Skip to content

Commit

Permalink
Improve discriminatorProperty documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthiasKunnen committed Jan 23, 2021
1 parent 6d0c510 commit eb5e533
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/json-inheritance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,33 @@ export interface DiscriminatorPropertyOptions {
property: string;

/**
* An arrow function returning an object matching a discriminator to its subtype.
* An arrow function returning an object with as key the discriminator and value the type to
* instantiate.
*/
types: () => {[k: string]: Serializable<any>};
}

/**
* Handle subtype lookup by matching the value of the given property to the types map. e.g.
* ```
* discriminatorProperty({
* property: 'type',
* types: () => ({
* foo: Foo,
* bar: Bar,
* }),
* })
* ```
* with the following data:
* ```
* {
* "type": "foo"
* }
* ```
* This will result in TypedJSON looking up the value of `data.type`, here `foo`, in the map
* provided by `types`, resulting in an object of type `Foo`. When serializing `Foo`, the `type`
* property will be added with value `foo`.
*/
export function discriminatorProperty(
{property, types}: DiscriminatorPropertyOptions,
): ObjectInheritanceOptions {
Expand Down

0 comments on commit eb5e533

Please sign in to comment.