Type-Safe Service Tokens
Use typed tokens to avoid runtime resolution errors.
The String Token Problem
String tokens like "Logger" are easy to typo and carry no type information. The container cannot tell you that get("Loggr") is wrong, and the resolved type is often any.
Symbols as Tokens
Using symbols instead of strings gives each token a unique identity that cannot collide with another by accident. A central registry keeps them organized.
const TYPES = {
Logger: Symbol.for("Logger"),
Mailer: Symbol.for("Mailer"),
} as const;All lessons in this course
- Inversion of Control Basics
- Constructor Injection
- DI Containers with InversifyJS
- Type-Safe Service Tokens