I have staus enum like this
export enum Status {
Active = "Active",
Inactive = "Inactive",
}
and i have UserStatus like this
export enum UserStatus {
Active = Status.Active,
};
and i have one common dto that holds the status
export class CommonDto {
@ApiProperty({ description: "Status", required: true })
status: Status;
and in my user dto i am extending this class liek this
export class UserDto extends CommonDto {
@ApiProperty({ description: "Status", required: true, enum: UserStatus })
status: UserStatus;
but this is showing error
Property 'status' in type 'UserDto' is not assignable to the same property in base type 'CommonDto'.
Type 'UserStatus' is not assignable to type 'Status'
any help?
There are some ways to solve it.
In this case we set the type of the status
generic. So you can give each class that extends from CommonDto a own type for status.
export class CommonDto<T> {
@ApiProperty({ description: "Status", required: true })
status: T;
// ...
}
export class UserDto extends CommonDto<UserStatus> {
// ...
}
You can set multiple, possible types in typescript like this:
export type AnyStatus = Status | UserStatus;
export class CommonDto {
@ApiProperty({ description: "Status", required: true })
status: AnyStatus;
// ...
}
export class UserDto extends CommonDto {
@ApiProperty({ description: "Status", required: true, enum: UserStatus })
status: AnyStatus;
// ...
}
In typescript you can "delete" properties and "add" properties in extended classes to maniupulate it types, like this:
type WithUserStatus<T> = Omit<T, 'status'> & { status: UserStatus };
type UserDto = WithUserStatus<CommonDto>;
Read more about intesection
These are the options you have to go. In your case I would set status in each class (except the base class (CommonDto)) separately with its own type.