handle another circular dependency scenario in DI

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™ 2025-01-13 17:51:52 +01:00
parent 66be5cd907
commit 5776439ac8
No known key found for this signature in database

View file

@ -96,9 +96,14 @@ class ContainerClass {
} else {
const paramTypes = (Reflect.getMetadata('design:paramtypes', type) ??
[]) as Constructable[];
const dependencies = paramTypes.map(<P>(paramType: Constructable<P>) =>
this.get(paramType),
);
const dependencies = paramTypes.map(<P>(paramType: Constructable<P>, index: number) => {
if (paramType === undefined) {
throw new DIError(
`Circular dependency detected in ${type.name} at index ${index}.\n${resolutionStack.map((t) => t.name).join(' -> ')}\n`,
);
}
return this.get(paramType);
});
// Create new instance with resolved dependencies
instance = new (type as Constructable)(...dependencies) as T;
}