Use Service Tokens for type-safe container access
developService tokens are unique identifiers that provide type-safe access to values stored in a Container. By using the Token<T> class, you ensure that the value retrieved from the container matches the expected type T at compile time.
import 'reflect-metadata';
import { Container, Token } from 'typedi';
// Define a typed token
export const JWT_SECRET_TOKEN = new Token<string>('MY_SECRET');
// Set a value using the token
Container.set(JWT_SECRET_TOKEN, 'wow-such-secure-much-encryption');
// Retrieve the value type-safely
const JWT_SECRET = Container.get(JWT_SECRET_TOKEN);