Skip to content

Cannot inject null value dependency - "TypeInfo not known for undefined" #273

@Able1991

Description

@Able1991

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.js when accessing ctor.name on 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

https://github.com/Able1991/tsyringe-issue

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions