Function Optional

  • Experimental

    If the identifier cannot be found, substitute it with null.

    The constraints supported are listed in ResolutionConstraintFlag.

    Returns ResolutionConstraintFlag

    The resolution constraint bit-flag for Optional.

    Example

    This function can be used to construct a bitmask for use in the dependencies array of a service. While this can be used publicly, it is mainly for use within TypeDI; it allows you to configure specific constraints for use when resolving a specific service.

    const constraintBitMask = Optional() | Self();

    if (constraintBitMask & ResolutionConstraintFlag.Optional) {
    console.log('The dependency is optional.');
    }

    Example

    Here is an example of this constraint as part of a service declaration:

    const NAME = new Token<string>();

    @Service([
    [NAME, Optional()]
    ])
    class MyService {
    constructor (private name: string | null) {
    // If "NAME" isn't provided in the container the service is run under,
    // it will be substituted with null. Note that the container may not
    // be the exact one it was declared under, but a descendent.
    }
    }

    See

    ResolutionConstraintFlag

Generated using TypeDoc