@djodjonx/wiredi
    Preparing search index...

    Function defineBuilderConfig

    • A helper function to define a builder configuration with strict type inference and inheritance. Use this instead of manually casting with satisfies BuilderConfig or as const to ensure that useBuilder can correctly infer the available tokens.

      This function now supports extends to inherit from definePartialConfig definitions. Token collisions are strictly forbidden - each token must be unique across all partials and the main configuration to prevent accidental redefinition.

      Type Parameters

      • C = null

        The type of the context object (optional).

      • const Partials extends readonly TypedPartialConfig<C, any, any>[] = []

        The tuple of partial configs to extend.

      • const LocalInjections extends InjectionConfig<C> = InjectionConfig<C>

        The specific type of the local injections array (inferred).

      • const LocalListeners extends EventConfig | undefined = EventConfig | undefined

      Parameters

      • config: {
            builderId: string;
            extends?: Partials;
            injections: ValidateInjections<
                LocalInjections,
                ExtractTokensFromTypedPartials<Partials>[number],
            >;
            listeners?: ValidateListeners<
                LocalListeners,
                ExtractListenersFromPartials<Partials>,
            >;
        }

        The builder configuration object.

      Returns TypedBuilderConfig<
          C,
          [
              ...ExtractTokensFromTypedPartials<Partials>[],
              ...ExtractTokens<LocalInjections>[],
          ],
      >

      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);