0

I have two seperate issues here.

let's start with my enum:

export enum TableViewTypes {
    user = 'users',
    pitching = 'pithcing',
    milestones = 'milestones',
    mediaList = 'mediaList',
    contacts = 'contacts'
}

in my service i have a method, i need to use the tableType argument as the key when reassigning a value using the spread operator...

storeTableView(tableType: TableViewTypes, value: boolean) {
    this.storageService.userPreferences = {
        ...this.storageService.userPreferences,
        tableView: {
            ...this.storageService.userPreferences.tableView,
            /* tableType??? */: value
        }
    };
}

Not sure what the correct thing to do here is while maintaing my type checking. The value of this.storageService.userPreferences.tableView has a type interface of...

export interface TableViewModel {
    users: boolean;
    pitching: boolean;
    milestones: boolean;
    mediaList: boolean;
    contacts: boolean;
}
Sandra Willford
  • 3,459
  • 11
  • 49
  • 96
  • *"I have two seperate issues here."* That usually means you should ask two separate questions, typically one after another (e.g., wait until you understand the answer to the first, then post the second). – T.J. Crowder Jul 16 '18 at 18:32
  • 1
    Your first question is answered [here](https://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable) (no, just use `[tableType]` directly). Your second question is answered [here](https://stackoverflow.com/questions/38825948/object-with-anothers-objects-property-as-a-key): You'd use `[tableType]: value` in that `tableView` object initializer. Happy coding! – T.J. Crowder Jul 16 '18 at 18:34
  • Side note: `...` is not an operator. :-) People call it an operator a lot, but it isn't, and can't be, because what it does doesn't fit what operators can do. It's just primary syntax. – T.J. Crowder Jul 16 '18 at 18:35
  • 2
    thanks, marking this as duplicate actually solved my issue. I see now in this issue how to solve this! – Sandra Willford Jul 16 '18 at 18:38
  • 1
    :-) That's the goal of marking questions as duplicates. It's not to "get rid" of the question, it's just, the other question already has answers! But when it seems like it would help, a lot of folks will *also* comment to point out how those answers apply to your situation. Happy coding! – T.J. Crowder Jul 16 '18 at 18:55

0 Answers0