I have developed a Typescript DTO to ensure type safety for a list of strings generated 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 is throwing an error:
Type 'UserTags[]' cannot be assigned to type 'number | keyof UserTags[][]'.
It seems that the value of userTags
is
number | keyof UserTags[][]
This is the enum definition I am working with.
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',
}
In the User class, UserTags
is defined as:
@ApiProperty({ enum: UserTags, isArray: true, default: [] })
@Column('enum', { enum: UserTags, array: true, nullable: true, default: [] })
userTags: UserTags[]
How can I specify just the type of keyof
for a particular Enum value?