Skip to content

API Reference


API Reference / @dasch-ng/utils / provideClass

Function: provideClass()

provideClass<T>(provide, useClass, deps, multi?): StaticClassProvider

Defined in: provide-helpers.ts:125

Creates a type-safe class provider for Angular dependency injection.

This helper ensures that the provided class is compatible with the injection token type, preventing type mismatches at compile time rather than runtime.

Type Parameters

T

T

The type of the injection token

Parameters

provide

The injection token or class to provide for

InjectionToken<T> | Type<T>

useClass

Type<T>

The class to instantiate (must be compatible with type T)

deps

any[] = []

Optional array of dependencies to inject into the class constructor

multi?

boolean

Optional flag to enable multi-provider

Returns

StaticClassProvider

A type-safe StaticClassProvider object

Example

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

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

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

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

// Type-safe: ConsoleLogger implements Logger
provideClass(LOGGER, ConsoleLogger);

// With dependencies
provideClass(LOGGER, ConsoleLogger, [SomeDependency]);

// Type error: String does not implement Logger
// provideClass(LOGGER, String);

See

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

Released under the MIT License.