This page is also available in Italiano.

Continua in italiano

Lifetimes

TinyDI supports exactly two lifetimes, via the ServiceLifetime enum. There is no third option, and no automatic detection — you state the lifetime you want.

ts
enum ServiceLifetime {
  Singleton,
  Transient,
}

Singleton

The default. The first time a token is resolved, its factory runs once; every later resolve() call for the same token returns that same instance.

ts
container.registerFactory(ClockToken, () => new SystemClock()); // Singleton by default
 
container.resolve(ClockToken) === container.resolve(ClockToken); // true

registerInstance is always Singleton — you are handing the container an instance that already exists, so there is nothing else it could be.

Transient

A new instance is created on every resolve() call.

ts
container.registerFactory(RequestIdToken, () => crypto.randomUUID(), ServiceLifetime.Transient);
 
container.resolve(RequestIdToken) === container.resolve(RequestIdToken); // false

Choosing between them

No Scoped lifetime yet

This version deliberately ships only Singleton and Transient. A future Scoped lifetime (per-request instances) is a natural extension the design does not preclude, but it is not part of the current API.

to navigate to select Esc to close