I have created a Typescript DTO, where I want to ensure type safety for a list of strings (which are derived from an enum type.
export class ConnectedUserWithPhotosDTO extends UserWithPhotosDTO {
userTags: keyof User["userTags"][]
constructor(user: User, photos: ResponsePhotoDTO[]) {
super(user, photos)
console.log(user.userTags)
this.userTags = user.userTags;
}
}
The typescript compiler complains about the following error:
Type 'UserTags[]' is not assignable to type 'number | keyof UserTags[][]'.
apparently, the value of userTags evaluates to
number | keyof UserTags[][]
Here is the enum definition that i am defining.
export enum UserTags {
FITNESS = 'Fitness',
FOURTWENTY_FRIENDLY = '420 Friendly',
MEDITATION = 'Meditation',
DRINKS = 'Drinks',
DOGS = 'Dogs',
CATS = 'Cats',
FASHION = 'Fashion',
WINE_TASTING = 'Wine Tasting',
FOODIE = 'Foodie',
ART = 'Art',
PARTYING = 'Partying',
TRAVELIING = 'Travelling',
GAMING = 'Gaming',
}
Inside the User class the UserTags is defined as:
@ApiProperty({ enum: UserTags, isArray: true, default: [] })
@Column('enum', { enum: UserTags, array: true, nullable: true, default: [] })
userTags: UserTags[]
how can I define just the type of keyof for the specific Enum value?
Usertype. It's not clear what you are trying to do.userTags: UserTags[], but I'll explain in an answer