Skip to content

Commit eb5e533

Browse files
Improve discriminatorProperty documentation
1 parent 6d0c510 commit eb5e533

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

src/json-inheritance.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,33 @@ export interface DiscriminatorPropertyOptions {
3030
property: string;
3131

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

39+
/**
40+
* Handle subtype lookup by matching the value of the given property to the types map. e.g.
41+
* ```
42+
* discriminatorProperty({
43+
* property: 'type',
44+
* types: () => ({
45+
* foo: Foo,
46+
* bar: Bar,
47+
* }),
48+
* })
49+
* ```
50+
* with the following data:
51+
* ```
52+
* {
53+
* "type": "foo"
54+
* }
55+
* ```
56+
* This will result in TypedJSON looking up the value of `data.type`, here `foo`, in the map
57+
* provided by `types`, resulting in an object of type `Foo`. When serializing `Foo`, the `type`
58+
* property will be added with value `foo`.
59+
*/
3860
export function discriminatorProperty(
3961
{property, types}: DiscriminatorPropertyOptions,
4062
): ObjectInheritanceOptions {

0 commit comments

Comments
 (0)