0

What is wrong in inheritance in Typescript?

i create this class:

export class ExtendedService<T extends ExtendedEntity> {
    protected repository: Repository<T>;

    constructor(
        repository?: Repository<T>
    ){
        this.repository = repository;
    }

    public async create(data: DeepPartial<T>): Promise<T> {
        const entity: T = this.repository.create(data);
        return entity.save();
    }
}

and extends this class in this one:

@Injectable()
export class UserService extends ExtendedService<UserEntity> {

    constructor(
        protected readonly repository: Repository<UserEntity>
    ){
        super();
    }

    public async register(data: DeepPartial<UserEntity>): Promise<UserEntity> {
        const user = new UserEntity();
        user.createdAt = new Date();
        user.firstName = data.firstName;
        user.lastName = data.lastName;
        user.email = data.email;
        user.role = UserRole.USER;
        user.isDeleted = false;
        
        const salt = await genSalt(10);
        user.password = await hash(data.password, salt);

        return await this.create(user);
    }
}

but after launch this function, my console throw me this:

[Nest] 19980   - 2020-06-29 17:30:56   [ExceptionsHandler] Cannot read property 'create' of undefined +1378ms
TypeError: Cannot read property 'create' of undefined
    at Repository.create (C:\Users\Piotruś\Desktop\auth\node_modules\typeorm\repository\Repository.js:49:29)
    at UserService.create (C:\Users\Piotruś\Desktop\auth\dist\helpers\service\extended-service.js:27:40)
    at UserService.register (C:\Users\Piotruś\Desktop\auth\dist\user\user.service.js:34:27)
    at async UserController.register (C:\Users\Piotruś\Desktop\auth\dist\user\user.controller.js:25:9)
    at async C:\Users\Piotruś\Desktop\auth\node_modules\@nestjs\core\router\router-execution-context.js:46:28
    at async C:\Users\Piotruś\Desktop\auth\node_modules\@nestjs\core\router\router-proxy.js:9:17

so, the problem is in ts inheritance, can anybody tell me what is wrong with my inheritance?

7
  • 2
    You're looking at the wrong create call. const entity: T = this.repository.create(data); is where the issue is. this.repository is undefined. Commented Jun 29, 2020 at 16:31
  • @zzzzBov: so, can you show what to change in code? Commented Jun 29, 2020 at 16:35
  • The error is in Repository, but you're not showing its code. Nothing to do with inheritance... Commented Jun 29, 2020 at 16:36
  • Repository is a typeorm class Commented Jun 29, 2020 at 16:38
  • Is it working without inheritance? Commented Jun 29, 2020 at 16:44

1 Answer 1

2

You're not passing the repository parameter down to your base class. You must supply all base class arguments when you call super() in your constructor.

@Injectable()
export class UserService extends ExtendedService<UserEntity> {

    constructor(
        protected readonly repository: Repository<UserEntity>
    ){
        super(repository);
    }
...
}

As you can see in this recreation, making the baseclass repository argument mandatory, causes Typescript to throw a compilation error:

Expected 1 arguments, but got 0.

I would recommend you to avoid making arguments optional unless it's necessary, and if you do, take precautions to make the necessary null checks.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.