Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nest dependency injection can't resolve type-only imports #5421

Closed
vegerot opened this issue Sep 14, 2020 · 1 comment
Closed

Nest dependency injection can't resolve type-only imports #5421

vegerot opened this issue Sep 14, 2020 · 1 comment
Labels
needs triage This issue has not been looked into

Comments

@vegerot
Copy link
Contributor

vegerot commented Sep 14, 2020

See typescript-eslint/typescript-eslint#2559 for discussion there

Bug Report

Since Typescript 3.8, Typescript has supported type-only imports and exports. This feature is incompatible with constructor dependency injection.

Current behavior

If you do a type-only import, you get an error like

Error: Nest can't resolve dependencies of the AppController (?, Function, Function, Function, UserSessionRepository). Please make sure that the argument Function at index [0] is available in the RootTestModule context.

Input Code

import {
  Controller,
} from '@nestjs/common';
import type { Logger } from '@nestjs/common';
import type { ConfigService } from './config/config.service';
import { UserSessionRepository } from '../entities/user-session/user.session.repository';
import type { UsersService } from './users/users.service';
import type { AuthService } from './auth/auth.service';

@Controller()
export class AppController {
  private readonly LANDING_URL: string;
  private readonly ROUTE: string;
  private readonly OAUTH_GATEWAY: string;

  constructor(
    private readonly config: ConfigService,
    private readonly usersService: UsersService,
    private readonly logger: Logger,
    private readonly authService: AuthService,
    @InjectRepository(UserSessionRepository)
    private readonly userSessionRepository: UserSessionRepository,
  ) {
    this.LANDING_URL = config.get('LANDING_URL');
    this.ROUTE = config.get('ROUTE');
    this.OAUTH_GATEWAY = config.get('OAUTH_GATEWAY');

Expected behavior

The dependencies should be injected as per usual.

Possible Solution

nest.js should be able to resolve type-only imports. Possibly by doing its injection earlier in the typescript build phase

Environment


Nest version: 7.4.4

Others:
Please see https://github.com/typescript-eslint/typescript-eslint/issues/2559 for discussion there too
@vegerot vegerot added the needs triage This issue has not been looked into label Sep 14, 2020
@jmcdo29
Copy link
Member

jmcdo29 commented Sep 14, 2020

The dependency injection runs during the use of node, not of tsc. This means that what's happening during the build phase is simply just transpiling Typescript into JavaScript, during this time, typed imports are checked for proper types, and then become {} just like interfaces. This isn't something that Nest will be able to get around, but rather something that should be taken into account during development. If you want to use class interfaces, or injection tokens with custom providers, that's something that can be managed, but Nest's DI system cannot get around what Typescript does with import type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage This issue has not been looked into
Projects
None yet
Development

No branches or pull requests

3 participants