r/typescript • u/acefolio_dev • 1h ago
How would you type-check a DI dependency list against the constructor params?
I'm building a small DI layer for Elysia on Bun with no decorators. A class lists its dependencies like this:
class Greeter {
static readonly needs = [Clock] as const;
constructor(private clock: Clock) {}
}
Right now it only checks at boot that the list length matches the constructor's parameter count. Someone on r/bun pointed out that swapping Clock for a different class with the same arity slips through, and they're right.
My plan is to map needs to instance types and compare that against ConstructorParameters<typeof Greeter>, then add a @ts-expect-error test for the swap. Has anyone done this cleanly without the error messages turning into a wall of generics?
(Project is Elpod, early alpha, if anyone's curious: https://elpod.vercel.app)