The type of the context object (optional).
The tuple of partial configs to extend.
The specific type of the local injections array (inferred).
The builder configuration object.
The configuration object typed as TypedBuilderConfig to simplify IDE display.
import { Lifecycle } from 'tsyringe';
class MyService {}
class MyProvider {}
class MyEvent {}
class MyEventListener {
onEvent(event: MyEvent) {
console.log('Event received:', event);
}
}
const MY_TOKEN = Symbol('MY_TOKEN');
const MY_VALUE_TOKEN = Symbol('MY_VALUE_TOKEN');
const MY_FACTORY_TOKEN = Symbol('MY_FACTORY_TOKEN');
// --- Partial Configuration ---
const myPartial = definePartialConfig({
injections: [
{ token: MyService } // Provides MyService
],
listeners: [
{ event: MyEvent, listener: MyEventListener }
]
});
// --- Main Builder Configuration ---
const myBuilderConfig = defineBuilderConfig({
builderId: 'my.unique.builder',
extends: [myPartial],
injections: [
// ❌ ERROR: Token collision - MyService is already defined in myPartial
// { token: MyService },
// ✅ OK: New tokens not in partials
{ token: MY_TOKEN, provider: MyProvider },
{ token: MY_TOKEN, provider: MyProvider, lifecycle: Lifecycle.Transient },
// 3. Value Injection (can use optional context)
{ token: MY_VALUE_TOKEN, value: (context) => context?.someConfig ?? 'defaultValue' },
// 4. Factory Injection
{ token: MY_FACTORY_TOKEN, factory: (container) => new MyService() },
],
listeners: [
// ❌ ERROR: Duplicate listener (Event + Listener pair already in myPartial)
// { event: MyEvent, listener: MyEventListener },
// ✅ OK: New listener not in partials
{ event: OtherEvent, listener: OtherEventListener },
],
});
// Usage:
// const { resolve } = useBuilder(myBuilderConfig, { someConfig: 'custom' });
// const service = resolve(MyService);
// const value = resolve(MY_VALUE_TOKEN);
A helper function to define a builder configuration with strict type inference and inheritance. Use this instead of manually casting with
satisfies BuilderConfigoras constto ensure thatuseBuildercan correctly infer the available tokens.This function now supports
extendsto inherit fromdefinePartialConfigdefinitions. Token collisions are strictly forbidden - each token must be unique across all partials and the main configuration to prevent accidental redefinition.