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
createToken<IClock>('Clock')creates a type-safe identifier. The generic<IClock>is the only place the type is written down — everything downstream infers it.container.registerFactory(...)tells the container how to build the service, lazily, the first time it is asked for.ServiceLifetime.Singletonis the default and could be omitted here.container.resolve(ClockToken)returns the service, typed asIClockwith 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.