fivem-ts

a library to speed up the development of Fivem scripts and frameworks


Project maintained by Purpose-Dev Hosted on GitHub Pages — Theme by mattgraham

fivem-ts - Documentation v0.7.5Docs


fivem-ts - Documentation v0.7.5 / Server / Inject

Function: Inject()

Inject(target, propertyKey, parameterIndex): void

Injects a dependency into a class property or constructor parameter.

This decorator is used to automatically inject dependencies into class properties or constructor parameters by resolving them from the DI container. It works by leveraging TypeScript’s metadata reflection to determine the type of dependency needed and then retrieving it from the global DI container.

Note: To use this decorator effectively, ensure that the emitDecoratorMetadata option is enabled in your tsconfig.json configuration file.

Example usage:

class MyService {
    constructor(@Inject private dependency: AnotherService) {}
}

// Ensure that AnotherService is registered with the DI container
container.registerSingleton(AnotherService);

// Resolve an instance of MyService with its dependencies injected
const myService = container.resolve(MyService);

Parameters

target: unknown

The prototype of the class or the constructor function where the dependency is injected.

propertyKey: string | symbol

The name of the property or method parameter where the dependency is injected.

parameterIndex: number

The index of the constructor parameter to be injected with a dependency. This is relevant only for constructor parameters.

Returns

void

Throws

Error if the dependency cannot be resolved. This might occur if:

Defined in

server/ioc/Inject.ts:36