简体   繁体   中英

What is wrong with Inheritance in Typescript?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM