-
Notifications
You must be signed in to change notification settings - Fork 180
Open
Labels
Description
Summary
TSyringe throws a runtime error when trying to inject dependencies with null values, even when properly registered in the container.
Environment
- TSyringe version: 4.8.0
- TypeScript version: 5.5.2
- Node.js version: 20.x
Minimal Reproduction
import "reflect-metadata";
import { container, injectable, inject } from "tsyringe";
interface User {
id: string;
email: string;
}
@injectable()
class UserService {
constructor(@inject("currentUser") private currentUser: User | null) {}
}
// Register dependency with null value
container.register("currentUser", { useValue: null });
// This throws an error!
const userService = container.resolve(UserService);Error:
Cannot inject the dependency "currentUser" at position #0 of "UserService" constructor. Reason:
TypeInfo not known for "undefined"
Expected Behavior
The code should work without errors and inject null as the value for currentUser.
Actual Behavior
TSyringe throws a runtime error because it tries to treat the null value as a class constructor.
Additional Notes
- The error persists even when using
@inject("currentUser", { isOptional: true }) - This is a common pattern in web applications for optional user authentication
- The error occurs in
dependency-container.jswhen accessingctor.nameon a null value
Use Cases
This pattern is needed for:
- GraphQL resolvers with optional authentication
- Express middleware for both authenticated and anonymous users
- Per-request dependency injection where user context might be null
- Services with different behavior for logged-in vs anonymous users
Reactions are currently unavailable