Skip to content

API Reference


API Reference / @dasch-ng/utils / provideExisting

Function: provideExisting()

provideExisting<T>(provide, useExisting, multi?): ExistingProvider

Defined in: provide-helpers.ts:171

Creates a type-safe existing provider (alias) for Angular dependency injection.

This helper creates an alias to an existing token, ensuring type compatibility between the original and alias tokens. Useful for providing the same instance under multiple tokens or creating aliases for better API design.

Type Parameters

T

T

The type of the injection token

Parameters

provide

The new injection token to create

InjectionToken<T> | Type<T>

useExisting

The existing token to alias (must be compatible with type T)

InjectionToken<T> | Type<T>

multi?

boolean

Optional flag to enable multi-provider

Returns

ExistingProvider

A type-safe ExistingProvider object

Example

typescript
import { Injectable, InjectionToken } from '@angular/core';
import { provideExisting } from '@dasch-ng/utils';

interface Logger {
  log(message: string): void;
}

const LOGGER = new InjectionToken<Logger>('LOGGER');
const CONSOLE_LOGGER = new InjectionToken<Logger>('CONSOLE_LOGGER');

@Injectable()
class ConsoleLogger implements Logger {
  log(message: string): void {
    console.log(message);
  }
}

// Provide both the original and an alias pointing to the same instance
const providers = [
  ConsoleLogger,
  provideExisting(LOGGER, ConsoleLogger),
  provideExisting(CONSOLE_LOGGER, LOGGER)
];

See

https://angular.dev/guide/di/dependency-injection-providers

Released under the MIT License.