Rohit Khatri
Rohit Khatri

Reputation: 2018

How to extend classes with same property in nestjs typescript

I'm extending AuthService class to UserAuthService class extracting common functionality so I can extend it to other services like AdminAuthService

Here's how the code looks like:

import { Inject, Injectable, UnauthorizedException } from "@nestjs/common";

export interface IUser {
    id: string;
    name: string;
    email: string;
    password: string;
}

export interface IUserService {
    findById(id: string): Promise<IUser | null>;
    findByEmail(email: string): Promise<IUser | null>;
    verifyPassword(user: IUser, password: string): Promise<Boolean>;
}


@Injectable()
export abstract class AuthService {
    // private userService: IUserService;

    constructor(private userService: IUserService) {
        // this.userService = userService;
    }

    async loginWithEmail(email: string, password: string) {
        const user = await this.userService.findByEmail(email);

        if (user.password !== password) {
            throw new UnauthorizedException('Invalid email & password!');
        }

        return user;
    }
}

export class UserAuthService extends AuthService {
    constructor(@Inject('user-service-token') private userService: IUserService) {
        super(userService);
    }
}

But I'm getting the below error:

Class 'UserAuthService' incorrectly extends base class 'AuthService'.
  Types have separate declarations of a private property 'userService'.

Can anyone help me resolve this?

Upvotes: 0

Views: 504

Answers (0)

Related Questions