Decorators and Types

Decorators

Injectable

  • Injectable(): (Anonymous function)
  • Placed just before the class declaration, this class decorator applies metadata to the class constructor indicating that the user intends to bind the class into the container. This decorator will throw if not placed on a class declaration, or if placed more than once on a class declaration.

    Returns (Anonymous function)

Inject

  • Placed just before a constructor parameter, this parameter decorator allows for specificity and control over the type of the type of Object that will be injected into the parameter. In the absence of this decorator the container will use whatever is bound to a parameter's type (or throw an error if it is unable to recognize the type).

    Parameters

    • id: InjectableId<any>

      The identifier of the bound type that should be injected.

    Returns (Anonymous function)

Optional

  • Optional(alt?: any): (Anonymous function)
  • Placed just before a constructor parameter, this parameter decorator signals the container that it should supply the 'alt' constant value (undefined by default) if for any reason it is unable to otherwise resolve the type of the parameter. WARNING! It is your responsibility to ensure that alt is of the appropriate type/value.

    Parameters

    • Optional alt: any

    Returns (Anonymous function)

PostConstruct

  • PostConstruct(): (Anonymous function)
  • Placed just before a class method, this method decorator flags a method that should be called after an object has been instantiated by the container, but before it is put into service. The method will be assumed to be synchronous unless the method signature explicitly declares it's return type to be ": Promise<something>" This decorator will throw if placed on a non-method or a static method of a class, or if placed on a method more than once, or if placed on more than one method for a class.

    Returns (Anonymous function)

Types

InjectableId

InjectableId<T>: string | symbol | { constructor: any }

Universal id that can be bound to a constant, class, or factories.

SyncFactory

SyncFactory<T>: (injector: Injector) => T

Type definition for functions that return a value. The function should return a valid value, but may throw an exception if it cannot.

Type declaration

AsyncFactory

AsyncFactory<T>: (injector: Injector) => Promise<T>

Type definition for functions that return a Promise for a value. The function must not throw and must return a valid Promise (e.g. pending, resolved, rejected).

Type declaration

    • Parameters

      Returns Promise<T>

ClassConstructor

ClassConstructor<T>: { constructor: any }

Standard definition of a constructor.

Type declaration

  • constructor: function
    • new __type(...args: any[]): T
    • Parameters

      • Rest ...args: any[]

      Returns T

OnErrorCallback

OnErrorCallback<T, M>: (injector: Injector, id: InjectableId<T>, maker: M, error: Error, value?: T) => T | Error | void

You may bind an error handler which will be invoked, if the bound InjectableId could not be put into service. An error handler must not throw, but may return an Error that will be propagated back up the call chain.

param

The Binder that experienced the error.

param

The identifier for what was trying to be made.

param

The thing that made (or tried to provideAsState). Will be one of type ClassConstructor, SyncFactory, or AsyncFactory, depending on how you registered the binding.

param

Identifies the problem that occurred.

param

If the 'maker' was able to create the thing, but it had an error during post construction, the made thing will be passed here.

returns

one of 3 results... A substitute thing (kind of like a 'maker' do-over) which must be fully operational (e.g. any @PostConstruct will be ignored). An alternate Error which will be propagated back up the call chain. Undefined, which means the 'error' parameter will be propagated back up the call chain.

Type declaration

    • Parameters

      Returns T | Error | void

Generated using TypeDoc