dependency-inversion_skill

This skill helps you apply the Dependency Inversion Principle in TypeScript by promoting interface-based design and injecting dependencies instead of
  • TypeScript

6

GitHub Stars

1

Bundled Files

2 months ago

Catalog Refreshed

4 months ago

First Indexed

Readme & install

Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.

Installation

Preview and clipboard use veilstrat where the catalogue uses aiagentskills.

npx veilstrat add skill yanko-belov/code-craft --skill dependency-inversion

  • SKILL.md6.3 KB

Overview

This skill detects violations of the Dependency Inversion Principle (DIP) where classes instantiate concrete dependencies internally instead of depending on abstractions. It flags uses of new ConcreteClass(), hardcoded configs, or direct imports of low-level implementations. It explains how to refactor to interfaces and constructor injection so code becomes testable and swappable.

How this skill works

The skill scans TypeScript classes and looks for patterns that indicate tight coupling: direct instantiation (new ...), hardcoded connection strings or API keys inside classes, and imports of concrete implementations used by high-level modules. For each finding it suggests an interface-based refactor: define an abstraction, implement it in a low-level module, and inject the dependency at the composition root. It also provides a short testing example using simple mocks.

When to use it

  • When a class calls new ConcreteService() or similar inside its body
  • When a module imports and uses concrete implementations directly
  • When a class requires real external services to be tested
  • When told to avoid dependency injection for simplicity
  • When configuration or credentials are hardcoded inside a class

Best practices

  • Always define an interface (abstraction) for external services your class uses
  • Accept dependencies via constructor parameters rather than instantiating them
  • Wire concrete implementations at the composition root (app startup)
  • Use lightweight test doubles (mocks/in-memory implementations) for unit tests
  • Avoid exceptions like 'we'll refactor later' — apply DI from the start

Example use cases

  • Refactor a UserService that new's an EmailService and Database into injected abstractions
  • Replace direct MySQL instantiation with a UserRepository interface and MySQL implementation
  • Turn a class with hardcoded API keys into one that receives a Config or Client via constructor
  • Enable unit tests by swapping real services for mock implementations
  • Make logging, caching, and persistence replaceable without touching business logic

FAQ

Yes — DI takes little extra effort and prevents tight coupling that grows costly as the project evolves.

What if I only have one implementation and it seems overkill?

One implementation is still coupling. Define the abstraction now; it keeps tests and future swaps simple.

How do I test classes after applying DIP?

Provide mock or in-memory implementations of the interfaces and inject them in tests to assert behavior without external services.

Built by
VeilStrat
AI signals for GTM teams
© 2026 VeilStrat. All rights reserved.All systems operational