Hey guys,
I am diving deeper into the typescript type system and thought that a custom typesafe dependency injection system would be the perfect starter project.
To get into the topic I have already done a little research (https://dev.to/vad3x/typesafe-almost-zero-cost-dependency-injection-in-typescript-112, https://github.com/nicojs/typed-inject).
I have come up with something that works a little like nestjs dependency injection and is heavily inspired by the inner workings of elysiajs.
To illustrate how it should work in the end I have setup this snippet:
const DatabaseService = new Service({
name: "DatabaseService",
description: "Database service which handles all database connections",
}).fn("doSomething", () => () => {
console.log("Hello World from Database Service!");
});
const ContactService = new Service({
name: "ContactService",
description: "Contact service which handles all contact related actions",
})
.service(DatabaseService)
.fn("createContact", ({ services: { DatabaseService } }) => () => {
console.log("Creating contact...");
DatabaseService.doSomething();
});
const ContactModule = new Module({
name: "ContactModule",
description: "Contact module which handles all contact related actions",
})
.service(DatabaseService)
.service(ContactService);
If I would remove the second last line (.service(DatabaseService)) the following .service() call with ContactService as argument should display a type error stating that the service requires a dependency which is not provided by the module.
Currently I am only able to set the return type to a string literal with the message, buy that of course does not underline the function call as error.