Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- enum FilterBy {
- COUNTRY = "country",
- AGE = "age",
- GENDER = "gender",
- FAVORITE = "favorite",
- }
- type FilterType<F extends FilterBy> = F extends FilterBy.AGE ? Number : (F extends FilterBy.GENDER ? Boolean : String)
- class Filter {
- filterBy: FilterBy;
- filterValue: FilterType<FilterBy>;
- constructor(filterBy: FilterBy, filterValue: FilterType<FilterBy>) {
- this.filterBy = filterBy;
- this.filterValue = filterValue;
- }
- public filter(users: Array<User>): Array<User> {
- return users.filter(user => user[this.filterBy.valueOf()] === this.filterValue );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement