βImports by interface
Implementation flexibility and prevent circular dependency.
Understand how it works.
export interface ICatsService {
getName(): string;
}import { Injectable } from '@nestjs/common';
import type { ICatsService } from './cats.interface';
@Injectable()
export class CatsService implements ICatsService {
getName() {
return 'Niro';
}
}import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { CatsService } from './cats/cats.service';
@Module({
controllers: [AppController],
providers: [
AppService,
{
provide: 'ICatsService',
useClass: CatsService,
},
],
})
export class AppModule {}Last updated