typedi

  • Version 0.10.0
  • Published
  • 433 kB
  • No dependencies
  • MIT license

Install

npm i typedi
yarn add typedi
pnpm add typedi

Overview

Dependency injection for TypeScript.

Index

Functions

function Inject

Inject: {
(): Function;
(typeFn: (type?: never) => Constructable<unknown>): Function;
(serviceName?: string): Function;
(token: Token<unknown>): Function;
};
  • Injects a service into a class property or constructor parameter.

function InjectMany

InjectMany: {
(): Function;
(type?: (type?: any) => Function): Function;
(serviceName?: string): Function;
(token: Token<any>): Function;
};
  • Injects a list of services into a class property or constructor parameter.

function Service

Service: {
<T = unknown>(): Function;
<T = unknown>(name: string): Function;
<T = unknown>(token: Token<unknown>): Function;
<T = unknown>(options?: ServiceOptions<T>): Function;
};
  • Marks class as a service that can be injected using Container.

Classes

class CannotInjectValueError

class CannotInjectValueError extends Error {}
  • Thrown when DI cannot inject value into property decorated by decorator.

constructor

constructor(target: Constructable<unknown>, propertyName: string);

    property message

    readonly message: string;

      property name

      name: string;

        class CannotInstantiateValueError

        class CannotInstantiateValueError extends Error {}
        • Thrown when DI cannot inject value into property decorated by decorator.

        constructor

        constructor(identifier: ServiceIdentifier<unknown>);

          property message

          readonly message: string;

            property name

            name: string;

              class Container

              class Container {}
              • Service container.

              property handlers

              static readonly handlers: Handler<unknown>[];
              • All registered handlers. The @Inject() decorator uses handlers internally to mark a property for injection.

              method get

              static get: {
              <T>(type: Constructable<T>): T;
              <T>(type: AbstractConstructable<T>): T;
              <T>(id: string): T;
              <T>(id: Token<T>): T;
              };
              • Retrieves the service with given name or type from the service container. Optionally, parameters can be passed in case if instance is initialized in the container for the first time.

              method getMany

              static getMany: { <T>(id: string): T[]; <T>(id: Token<T>): T[] };
              • Gets all instances registered in the container of the given service identifier. Used when service defined with multiple: true flag.

              method has

              static has: {
              <T>(type: Constructable<T>): boolean;
              <T>(id: string): boolean;
              <T>(id: Token<T>): boolean;
              };
              • Checks if the service with given name or type is registered service container. Optionally, parameters can be passed in case if instance is initialized in the container for the first time.

              method import

              static import: (services: Function[]) => Container;
              • Helper method that imports given services.

              method of

              static of: (containerId?: string) => ContainerInstance;
              • Gets a separate container instance for the given instance id.

              method registerHandler

              static registerHandler: (handler: Handler) => Container;
              • Registers a new handler.

              method remove

              static remove: (
              identifierOrIdentifierArray: ServiceIdentifier | ServiceIdentifier[]
              ) => Container;
              • Removes services with a given service identifiers.

              method reset

              static reset: (containerId?: string) => Container;
              • Completely resets the container by removing all previously registered services and handlers from it.

              method set

              static set: {
              <T = unknown>(type: Function, value: any): Container;
              <T = unknown>(type: Constructable<T>, value: any): Container;
              <T = unknown>(type: AbstractConstructable<T>, value: any): Container;
              <T = unknown>(name: string, value: any): Container;
              <T = unknown>(token: Token<T>, value: any): Container;
              <T = unknown>(value: ServiceOptions<T>): Container;
              <T = unknown>(values: ServiceOptions<T>[]): Container;
              };
              • Sets a value for the given type or service name in the container.

              class ContainerInstance

              class ContainerInstance {}
              • TypeDI can have multiple containers. One container is ContainerInstance.

              constructor

              constructor(id: string);

                property id

                readonly id: string;
                • Container instance id.

                method get

                get: {
                <T>(type: Constructable<T>): T;
                <T>(type: AbstractConstructable<T>): T;
                <T>(id: string): T;
                <T>(id: Token<T>): T;
                <T>(id: ServiceIdentifier<T>): T;
                };
                • Retrieves the service with given name or type from the service container. Optionally, parameters can be passed in case if instance is initialized in the container for the first time.

                method getMany

                getMany: {
                <T>(type: Constructable<T>): T[];
                <T>(type: AbstractConstructable<T>): T[];
                <T>(id: string): T[];
                <T>(id: Token<T>): T[];
                <T>(id: ServiceIdentifier<T>): T[];
                };
                • Gets all instances registered in the container of the given service identifier. Used when service defined with multiple: true flag.

                method has

                has: {
                <T>(type: Constructable<T>): boolean;
                <T>(id: string): boolean;
                <T>(id: Token<T>): boolean;
                };
                • Checks if the service with given name or type is registered service container. Optionally, parameters can be passed in case if instance is initialized in the container for the first time.

                method remove

                remove: (
                identifierOrIdentifierArray: ServiceIdentifier | ServiceIdentifier[]
                ) => this;
                • Removes services with a given service identifiers.

                method reset

                reset: (options?: { strategy: 'resetValue' | 'resetServices' }) => this;
                • Completely resets the container by removing all previously registered services from it.

                method set

                set: {
                <T = unknown>(service: ServiceMetadata<T>): this;
                <T = unknown>(type: Constructable<T>, instance: T): this;
                <T = unknown>(type: AbstractConstructable<T>, instance: T): this;
                <T = unknown>(name: string, instance: T): this;
                <T = unknown>(token: Token<T>, instance: T): this;
                <T = unknown>(token: ServiceIdentifier<unknown>, instance: T): this;
                <T = unknown>(metadata: ServiceOptions<T>): this;
                <T = unknown>(metadataArray: ServiceOptions<T>[]): this;
                };
                • Sets a value for the given type or service name in the container.

                class ServiceNotFoundError

                class ServiceNotFoundError extends Error {}
                • Thrown when requested service was not found.

                constructor

                constructor(identifier: ServiceIdentifier<unknown>);

                  property message

                  readonly message: string;

                    property name

                    name: string;

                      class Token

                      class Token<T> {}
                      • Used to create unique typed service identifier. Useful when service has only interface, but don't have a class.

                      constructor

                      constructor(name?: string);
                      • Parameter name

                        Token name, optional and only used for debugging purposes.

                      property name

                      name?: string;

                        Interfaces

                        interface Handler

                        interface Handler<T = unknown> {}
                        • Used to register special "handler" which will be executed on a service class during its initialization. It can be used to create custom decorators and set/replace service class properties and constructor parameters.

                        property index

                        index?: number;
                        • Parameter index to set/replace value of. Used if handler is applied on a constructor parameter.

                        property object

                        object: Constructable<T>;
                        • Service object used to apply handler to.

                        property propertyName

                        propertyName?: string;
                        • Class property name to set/replace value of. Used if handler is applied on a class property.

                        property value

                        value: (container: ContainerInstance) => any;
                        • Factory function that produces value that will be set to class property or constructor parameter. Accepts container instance which requested the value.

                        interface ServiceMetadata

                        interface ServiceMetadata<Type = unknown> {}
                        • Service metadata is used to initialize service and store its state.

                        property eager

                        eager?: boolean;
                        • Indicates whether a new instance should be created as soon as the class is registered. By default the registered classes are only instantiated when they are requested from the container.

                        property factory

                        factory: [Constructable<unknown>, string] | CallableFunction | undefined;
                        • Factory function used to initialize this service. Can be regular function ("createCar" for example), or other service which produces this instance ([CarFactory, "createCar"] for example).

                        property global

                        global: boolean;
                        • Indicates if this service must be global and same instance must be used across all containers.

                        property id

                        id: ServiceIdentifier;
                        • Unique identifier of the referenced service.

                        property multiple

                        multiple: boolean;
                        • Allows to setup multiple instances the different classes under a single service id string or token.

                        property transient

                        transient: boolean;
                        • Indicates whether a new instance of this class must be created for each class injecting this class. Global option is ignored when this option is used.

                        property type

                        type: Constructable<Type> | null;
                        • Class definition of the service what is used to initialize given service. This property maybe null if the value of the service is set manually. If id is not set then it serves as service id.

                        property value

                        value: unknown | Symbol;
                        • Instance of the target class.

                        Type Aliases

                        type Constructable

                        type Constructable<T> = new (...args: any[]) => T;
                        • Generic type for class definitions. Example usage:

                          function createSomeInstance(myClassDefinition: Constructable<MyClass>) {
                          return new myClassDefinition()
                          }

                        type ServiceIdentifier

                        type ServiceIdentifier<T = unknown> =
                        | Constructable<T>
                        | AbstractConstructable<T>
                        | CallableFunction
                        | Token<T>
                        | string;
                        • Unique service identifier. Can be some class type, or string id, or instance of Token.

                        type ServiceOptions

                        type ServiceOptions<T = unknown> =
                        | Omit<Partial<ServiceMetadata<T>>, 'type' | 'factory'>
                        | Omit<Partial<ServiceMetadata<T>>, 'value' | 'factory'>
                        | Omit<Partial<ServiceMetadata<T>>, 'value' | 'type'>;
                        • The public ServiceOptions is partial object of ServiceMetadata and either one of the following is set: type, factory, value but not more than one.

                        Package Files (15)

                        Dependencies (0)

                        No dependencies.

                        Dev Dependencies (0)

                        No dev dependencies.

                        Peer Dependencies (0)

                        No peer dependencies.

                        Badge

                        To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                        You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/typedi.

                        • Markdown
                          [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/typedi)
                        • HTML
                          <a href="https://www.jsdocs.io/package/typedi"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>