• @IfThen decorator determine if a Primitive passed as argument should be read with the associated property based on a condition passed as argument.

    Type Parameters

    • This extends object

      The type of the class the decorator is applied to.

    • Target

      The type of the relation

    • Value

      The type of the decorated property.

    • Args extends string

    Parameters

    • cond: ConditionFunction<This>

      A function that receives the instance of the class the property belongs in as a parameter and return a boolean.

    • Optionalthen: Primitive<Target>

      Property type to read if the 'cond' function pass.

    • Optionalargs: RelationParameters<This, Args>

      Optional arguments passed to the nested type definition if the 'cond' pass.

    Returns DecoratorType<This, Value>

    The property decorator function.

    In the following example the data property will be associated with a unsigned 16 bit integer if the value of the property type is equal to 0x01. If the condition is not met the data property will be left undefined.

    class Protocol {
    @Relation(PrimitiveSymbol.u8)
    type: number

    @IfThen(instance => instance.type === 0x01, PrimitiveSymbol.u16)
    @Else()
    data: number
    }

    The @IfThen decorators are executed with a top-down direction. This means the condition the further away from the property get executed first.

    class Protocol {
    @IfThen(_ => true, Foo) // This one get picked first.
    @IfThen(_ => true, Bar)
    @Else()
    condition: Foo | Bar
    }

    The @IfThen decorator is often used in conjunction with other conditional decorators like Else.