A tuple of InjectableFunctions.
// Define some Injectable functions
const injectable1 = Injectable("Service1", () => "service1");
const injectable2 = Injectable("Service2", () => 42);
// Collect them in a tuple
const injectables = [injectable1, injectable2] as const;
// Use ServicesFromInjectables to derive the services' types
type Services = ServicesFromInjectables<typeof injectables>;
// Services type is equivalent to:
// {
// Service1: string;
// Service2: number;
// }
// Declare a container variable with the derived Services type
// This allows us to reference the container with accurate typing before it's constructed,
// ensuring type safety and enabling its use in type annotations elsewhere
let container: Container<Services>;
// Assign the container with the actual instance
container = Container.provides(injectable1).provides(injectable2);
Maps an array of InjectableFunction to a service type object, where each key is the token of an Injectable, and the corresponding value is the return type of that Injectable.
This utility type is useful for deriving the service types provided by a collection of InjectableFunctions, ensuring type safety and consistency throughout your application.
You can use
ServicesFromInjectables
to construct a type that serves as a type parameter for a Container, allowing the container's type to accurately reflect the services it provides, even before the container is constructed.