简体   繁体   English

Typescript 中的 Inheritance 有什么问题?

[英]What is wrong with Inheritance in Typescript?

What is wrong in inheritance in Typescript? Typescript 中的 inheritance 有什么问题?

i create this class:我创建了这个 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:并在此扩展此 class :

@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:但是在启动这个 function 之后,我的控制台向我抛出了这个:

[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?所以,问题出在 ts inheritance 中,谁能告诉我我的 inheritance 出了什么问题?

You're not passing the repository parameter down to your base class.您没有将repository参数传递给您的基础 class。 You must supply all base class arguments when you call super() in your constructor.在构造函数中调用super()时,必须提供所有基本 class arguments。

@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:正如您在此娱乐中看到的那样,将基类存储库参数设为强制性会导致 Typescript 引发编译错误:

Expected 1 arguments, but got 0.预期为 1 arguments,但得到 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.除非必要,否则我建议您避免将 arguments 设为可选,如果这样做,请采取预防措施以进行必要的 null 检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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