NodeJS Nest无法解析TMPController的依赖项

1zmg4dgp  于 2023-05-28  发布在  Node.js
关注(0)|答案(1)|浏览(149)

无论我做什么,错误都在不断发生,尝试了我找到的每一种解决方案,但都无济于事。
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;
    };
};

先谢谢你了!

oyjwcjzk

oyjwcjzk1#

当类型与构造函数类型相关时,不要使用import type。Typescript将不会反映正确的类型,因此Nest将无法解析它需要的类型,因此它可以正确地注入服务。

相关问题