This page is also available in Italiano.

Continua in italiano

Quick Start

There is no hidden step in TinyDI. This page is the entire mental model: create a container, register a service, resolve it.

The example

ts
import { Container, createToken, ServiceLifetime } from 'tinydi-container';
 
interface IClock {
  now(): Date;
}
 
class SystemClock implements IClock {
  now(): Date {
    return new Date();
  }
}
 
const ClockToken = createToken<IClock>('Clock');
 
const container = new Container();
container.registerFactory(ClockToken, () => new SystemClock(), ServiceLifetime.Singleton);
 
const clock = container.resolve(ClockToken);
console.log(clock.now());

What is happening here

  1. createToken<IClock>('Clock') creates a type-safe identifier. The generic <IClock> is the only place the type is written down — everything downstream infers it.
  2. container.registerFactory(...) tells the container how to build the service, lazily, the first time it is asked for. ServiceLifetime.Singleton is the default and could be omitted here.
  3. container.resolve(ClockToken) returns the service, typed as IClock with no explicit generic.

No decorators, no reflection

Every wiring decision above is an ordinary function call you can Cmd+Click through. Nothing here relies on reflect-metadata, and there is no compiler flag to enable.

Next steps

to navigate to select Esc to close