Skip to content
GitHub

ClassProvider

interface in meocord/interface and meocord/testing Since 4.1.0

interface ClassProvider<T = any>

A provider that binds a class's instance under a token, made once and shared, with its own dependencies injected.

Use it to let classes inject an abstract class and receive one implementation of it, such as an in-memory store in development and a database-backed one in production.

Examples

TypeScript
abstract class NoteStore {
  abstract save(text: string): Promise<void>
}
class MemoryNoteStore extends NoteStore {
  readonly notes: string[] = []
  async save(text: string) {
    this.notes.push(text)
  }
}

// A class that injects NoteStore gets the one MemoryNoteStore
const notes: ClassProvider<NoteStore> = { provide: NoteStore, useClass: MemoryNoteStore }

Members

provide

provide: ProviderToken<T>

The token classes inject the instance by, often the abstract class it implements.

useClass

useClass: new (...args: any[]) => T

The class to make the instance from, whose constructor dependencies are injected.

See also