@Inject decorator
Last updated
Last updated
The @Inject()
decorator is a property and parameter decorator used to resolve dependencies on a property of a class or a constructor parameter. By default it infers the type of the property or argument and initializes an instance of the detected type, however, this behavior can be overwritten via specifying a custom constructable type, Token
, or named service as the first parameter.
This decorator is mandatory on properties where a class instance is desired (aka: without the decorator, the property will stay undefined). The type of the property is automatically inferred so there is no need to define the desired value as the decorator parameter.
The @Inject
decorator is not required in constructor injection when a class is marked with the @Service
decorator. TypeDI will automatically infer and inject the correct class instances for every constructor argument. However, it can be used to overwrite the injected type.
By default, TypeDI will try to infer the type of property and arguments and inject the proper class instance. When this is possible (eg: the property type is an interface) there is three-way to overwrite type of the injected value:
via @Inject(() => type)
where type
is a constructable value (eg: a class definition)
via @Inject(myToken)
where myToken
is an instance of Token
class
via @Inject(serviceName)
where serviceName
is a string which is already registered via Container.set(serviceName, value)