After setting "strict": true
in my tsconfig.json
, I encountered compiler errors when attempting to run the code. To replicate and explore this issue further, you can try running the following code snippet. The problem arises when the child class is deemed incompatible with the base class. Could there be a potential solution that I am overlooking or perhaps an error in my approach?
export class AbstractDto {
id: string;
createdAt: Date;
updatedAt: Date;
constructor(entity: AbstractEntity) {
this.id = entity.id;
this.createdAt = entity.createdAt;
this.updatedAt = entity.updatedAt;
}
}
export abstract class AbstractEntity<T extends AbstractDto = AbstractDto> {
id: string = '';
createdAt: Date = new Date();
updatedAt: Date = new Date();
abstract dtoClass: new (entity: AbstractEntity, options?: any) => T;
}
export class UserEntity extends AbstractEntity<AbstractDto> {
firstName: string = '';
one: string = '';
dtoClass = UserDto;
// ^^^^^^^^ - Property 'dtoClass' in type 'UserEntity'
// is not assignable to the same property in base type 'AbstractEntity<AbstractDto>'.
}
export class UserDto extends AbstractDto {
one: string = '';
constructor(user: UserEntity) {
super(user);
// ^^^^ - Argument of type 'UserEntity'
// is not assignable to parameter of type 'AbstractEntity<AbstractDto>'.
this.one = user.one;
}
}