Search code examples
node.jstypescriptnestjs

Nest can't resolve dependencies of the TMPController


The error keeps happening no matter what I do, tried every solution that I found but nothing helps.

tmp.module.ts

import { Module } from "@nestjs/common";
import { TMPController } from "./tmp.controller";
import { TMPService } from "./tmp.service";

@Module({
    controllers: [TMPController],
    providers: [TMPService],
})
export class TMPModule { };

tmp.controller.ts

import { Controller, Get } from "@nestjs/common";
import type { APICompanyMembers, APICompanyNews } from "@truckersmp_official/api-types/v2";
import type { TMPService } from "./tmp.service";

@Controller("tmp")
export class TMPController {
    constructor(private readonly tmpService: TMPService) { };

    @Get("news")
    async findNews(): Promise<{ response: APICompanyNews }> {
        return await this.tmpService.getNews();
    };
};

tmp.service.ts

import { Injectable } from "@nestjs/common";
import type { APICompanyMembers, APICompanyNews } from "@truckersmp_official/api-types/v2";
import http from "../../lib/http";

@Injectable()
export class TMPService {
    async getNews(): Promise<{ response: APICompanyNews }> {
        const data = (await http.get<{ response: APICompanyNews }>("https://api.truckersmp.com/...")).data;

        return data;
    };
};

Thanks in advance!


Solution

  • Don't use import type when the type is related to your constructor types. Typescript will not reflect the proper type so Nest will not be able to resolve what it needs to so it can inject the service properly.