node.jstypescripttsyringe

Cannot inject the depency at position #0 because TypeInfo not known


I'm having this trouble:

[ERROR] 14:07:18 Error: Cannot inject the dependency at position #0 of "ListInterviewPagesUseCase" constructor. Reason: TypeInfo not known for "InterviewPagesRepository"

Container

import { container } from "tsyringe";

import { InterviewPagesRepository } from "../../modules/interview/repositories/implementations/InterviewPagesRepository";

import "./providers";

container.registerSingleton(
  "InterviewPagesRepository",
  InterviewPagesRepository
);

Controller

import { Request, Response } from "express";
import { container } from "tsyringe";

import { ListInterviewPagesUseCase } from "./ListInterviewPagesUseCase";

class ListInterviewPagesController {
  async handle(request: Request, response: Response): Promise<Response> {
    const listInterviewPagesUseCase = container.resolve(
      ListInterviewPagesUseCase
    );

    const interviewPages = await listInterviewPagesUseCase.execute();

    return response.json(interviewPages);
  }
}

export { ListInterviewPagesController };

UseCase

import { inject, injectable } from "tsyringe";

import { InterviewPage } from "../../entities/InterviewPage";
import { IInterviewPagesRepository } from "../../repositories/IInterviewPagesRepository";

@injectable()
class ListInterviewPagesUseCase {
  constructor(
    @inject("InterviewPagesRepository")
    private interviewPagesRepository: IInterviewPagesRepository
  ) {}

  async execute(): Promise<InterviewPage[]> {
    const interviewPages = await this.interviewPagesRepository.all();

    return interviewPages;
  }
}

export { ListInterviewPagesUseCase };

It was working normally on windows, but now I've changed to mac and stopped working. I already put delay from tsrynge but still doesn`t work.

This happens when I call a route that use this repository, but seems to look ok, the database is created.

My imports in app file:

import "reflect-metadata";
import "dotenv/config";
import "./database";
import "./shared/container";

import cors from "cors";
import express from "express";
import swaggerUi from "swagger-ui-express";

import { router } from "./routes";
import swaggerFile from "./swagger.json";

Solution

  • Consider decorating InterviewPagesRepository with @singleton, instead of calling container.registerSingleton yourself:

    import { singleton } from "tsyringe";
    
    @singleton()
    class InterviewPagesRepository {
    

    The @singleton decorator does invoke registerSingleton() inside, like you do, but it also stores it to be used in parameters of other constructors (see 12, and then 345).