/** * Represents a newable class. * @typeParam T Class type. */ interface Newable extends Function { new (...args: any[]): T; } /** * Represents an abstract class. * @typeParam T Class type. */ interface Abstract extends Function { prototype: T; } /** * Service identifier. Can be a concrete implementation or an abstraction. * @typeParam T Class type. */ type Identifier = Newable | Abstract; /** * Creates, wires dependencies and manages lifetime for a set of services. * Instances of Container are created by a {@link ContainerBuilder}. */ interface Container { /** * Gets the service object of the registered identifier. * @param identifier Class of the service to get. * @typeParam T The type of the service. * @returns */ get(identifier: Identifier): T; /** * Returns service ids for a given tag. * @param tag The tag name. * @typeParam T The type of the returned services. * @returns An array of service identifiers tagged with the given tag. */ findTaggedServiceIdentifiers(tag: string): Identifier[]; } /** * Represents a factory to create instances of a class. * The {@link Container } will be passed to the registered factory to be able to * get any other service. * @typeParam T Class type. */ type Factory = (container: Container) => T; /** * Represents an instance of a class. * @typeParam T Class type. */ type Instance = T & Object; /** * Options for the {@link Container} build method. */ type BuildOptions = { /** Whether to autowire dependencies based on types or not. Default value: `true`. */ autowire?: boolean; }; /** * Configuration that allows scope change */ interface ConfigurableRegistration { /** * The service can be used as a dependency and it can be queried from the container. * @returns */ public(): this; /** * The service can only be used as a dependency and it can't be queried from the container. * @returns */ private(): this; /** * Tag the service (the tag will be added to previously added tags if there are). * @returns */ addTag(tag: string): this; } interface WithScopeChange { /** * Configure the service so that always gets a new instance. * @returns */ asTransient(): this; /** * Configure the service so that always gets the same, shared instance. * @returns */ asSingleton(): this; /** * Configure the service so that the same shared instance is used during * within a {@link Container}.get request. * @returns */ asInstancePerRequest(): this; } /** * Configuration that allows to manage dependencies manually */ interface WithDependencies { /** * Declare class dependencies manually * @param dependencies List of class dependency identifiers to inject in * order to the constructor. * @returns */ withDependencies(dependencies: Identifier[]): this; } interface Registration { /** * Configure the class implementation that the identifier will provide. * @param newable The implementation that the identifier will provide. * @returns Configuration fluent API for classes */ useClass(newable: Newable): ConfigurableRegistration & WithScopeChange & WithDependencies; /** * Configure the class implementation that the identifier will provide. * Alias of `useClass`. * @param newable The implementation that the identifier will provide. * @returns Configuration fluent API for classes */ use(newable: Newable): ConfigurableRegistration & WithScopeChange & WithDependencies; /** * Configure the instance that the identifier will provide. * @param instance The instance that the identifier will provide. * @returns Configuration fluent API for instances */ useInstance(instance: Instance): ConfigurableRegistration; /** * Configure a factory that returns the instance that the identifier will provide. * @param factory The factory that will be executed when the identifier is requested. * @returns Configuration fluent API for factories */ useFactory(factory: Factory): ConfigurableRegistration & WithScopeChange; } /** * Used to build an {@link Container} from service registrations. */ declare class ContainerBuilder { private readonly buildables; /** * Registers a service. * @param identifier The class that identifies this service. This class * identifier must be used to get the service from the container or when * defining it as a dependency. * @typeParam T The type of the service. * @returns */ register(identifier: Identifier): Registration; /** * Unregister previously registered service. * @param identifier The class that identifies this service to be unregistered. * @typeParam T The type of the service. */ unregister(identifier: Identifier): void; /** * Checks whether a service is registered or not. * @param identifier The class that identifies this service to be checked. * @typeParam T The type of the service. * @returns */ isRegistered(identifier: Identifier): boolean; /** * Alias for `.register(newable).use(newable)`. * @param newable The concrete class implementation to be registered as itself. * @typeParam T The type of the service. * @returns */ registerAndUse(newable: Newable): ConfigurableRegistration & WithScopeChange & WithDependencies; /** * Builds an immutable {@link Container}. * @param options Build options. * @returns */ build({ autowire }?: BuildOptions): Container; } /** * Decorator for injectable classes. Every registered service must * be decorated because without decorators Typescript won't emit * constructor metadata. * @returns */ declare const Service: () => ClassDecorator; export { type Abstract, type BuildOptions, type ConfigurableRegistration, type Container, ContainerBuilder, type Factory, type Identifier, type Instance, type Newable, type Registration, Service, type WithDependencies, type WithScopeChange };