Skip to main content

Optional Dependencies

You can declare an optional dependency using the standard TypeScript syntax:

ApiService.ts
import { injected } from 'brandi';
import { TOKENS } from './tokens';
import { Logger } from './Logger';
export class ApiService {
/* โ†“ The optional dependency. */
constructor(private logger?: Logger) {}
}

And inject it using the optional token field:

ApiService.ts
injected(
ApiService,
TOKENS.logger.optional /* โ† The optional dependency token. */,
);

When using optional tokens, the Brani container will not throw an error if the dependency is not bound in the container, but will return undefined.