This repository contains a step-by-step guide on using TSyringe for dependency injection in a TypeScript project. TSyringe is a powerful and easy-to-use dependency injection library that helps create modular, maintainable, and testable code.
src/
├── services/
│ ├── EmailService.ts
│ └── NotificationService.ts
├── main.ts
- Node.js installed
- npm installed
-
Clone the repository:
git clone https://github.com/yourusername/tsyringe-tutorial.git cd tsyringe-tutorial
-
Install dependencies:
npm install
-
Initialize TypeScript configuration (if not already done):
npx tsc --init
-
Configure
tsconfig.json
:Make sure the following options are enabled in your
tsconfig.json
:{ "compilerOptions": { "target": "es6", "module": "commonjs", "outDir": "./dist", "rootDir": "./src", "strict": true, "esModuleInterop": true, "experimentalDecorators": true, "emitDecoratorMetadata": true }, "include": ["src"] }
-
Build the project:
npx tsc
This will compile your TypeScript files into JavaScript and place them in the
dist
directory. -
Run the project:
node dist/main.js
You should see the following output:
Email sent with message: Hello, Dependency Injection with TSyringe!
import 'reflect-metadata';
import { injectable } from 'tsyringe';
@injectable()
class EmailService {
sendEmail(message: string) {
console.log(`Email sent with message: ${message}`);
}
}
export default EmailService;
import { inject, injectable } from 'tsyringe';
import EmailService from './EmailService';
@injectable()
class NotificationService {
constructor(
@inject('EmailService') private emailService: EmailService
) {}
sendNotification(message: string) {
this.emailService.sendEmail(message);
}
}
export default NotificationService;
import 'reflect-metadata';
import { container } from 'tsyringe';
import EmailService from './services/EmailService';
import NotificationService from './services/NotificationService';
// Register the EmailService with the container
container.register('EmailService', { useClass: EmailService });
// Resolve the NotificationService, which has a dependency on EmailService
const notificationService = container.resolve(NotificationService);
// Use the NotificationService to send a notification
notificationService.sendNotification('Hello, Dependency Injection with TSyringe!');
Feel free to fork this repository, make improvements, and submit a pull request. Any contributions are welcome!
This project is licensed under the MIT License.