Type to represent multiple possible values I created this type for use in a "Wizard"-style user interaction. In each step, the user chooses an option called a Modifier and each of those options contains subselections that must be made.

These Modifiers are stored with two parallel structures, one of the fixed values and a second of the selectable ones using this type. When the uses chooses a suboption, the chosen value is merged into the fixed values and when completed, the values are used to modify the wizard model.

/**
 *  A portion of a modifier that consists of a set of possible values and a number of them
 *  that will be selected. After selection, the selected values will be added to the set values
 *  of the modifier.
 */
export class SelectableModifier {

    constructor(numSelectionsNeeded:number, options:Array&lt;any&gt;, selectionTime:string) {
        this._numSelectionsNeeded = numSelectionsNeeded;
        this._options = options;
        this._selectionTime = selectionTime;
    }

    /**
     * The number of options that need to be selected.
     */
    private _numSelectionsNeeded:number;
    /**
     * The available options
     */
    private _options:Array&lt;any&gt;;

    private _selectionTime:string;

    /**
     * Choose from this selection, decomposing it into the selected options.
     *
     * @param chosenIndices
     */
    public makeSelection(chosenIndices:Array&lt;Number&gt;):Array&lt;any&gt; {
        if (chosenIndices.length != this._numSelectionsNeeded) {
            throw "The selection requires that " + this._numSelectionsNeeded + " selections be made but " + chosenIndices.length + " were instead."
        }
        return this._options.filter((element, index)=&gt; {
            return chosenIndices.indexOf(index) !== -1;
        });
    }

    get numSelectionsNeeded():number {
        return this._numSelectionsNeeded;
    }

    get options():Array&lt;any&gt; {
        return this._options;
    }

    get selectionTime():string {
        return this._selectionTime;
    }
}
How about this?


Use constructor(private _options: any[]) instead of field declaration and assignment.
As a defensive programming measure use = [] to initialize the array with an empty value in null/undefined was padded in.
Instead of numerous searches through chosenIndices (via indexOf) use a simple .map(...) function instead.




export class SelectableModifier {

  constructor(private _numSelectionsNeeded: number, private _options: any[] = [], private _selectionTime: string) { }

  get numSelectionsNeeded(): number {
    return this._numSelectionsNeeded;
  }

  get options(): any[] {
    return this._options;
  }

  get selectionTime(): string {
    return this._selectionTime;
  }

  public makeSelection(chosenIndices: number[]): any[] {
    if (chosenIndices.length != this.numSelectionsNeeded) {
      throw new Error("The selection requires that " + this._numSelectionsNeeded + " selections be made but " + chosenIndices.length + " were instead.");
    }

    return chosenIndices.map(indexOfChosen =&gt; this.options[indexOfChosen]);
  }
}




UPD1

If null/undefined choices should be treated as errors, you can check for their presence and throw.

public makeSelection(chosenIndices: number[]): any[] {
    if (chosenIndices.length != this.numSelectionsNeeded) {
        throw new Error(`The selection requires that ${this._numSelectionsNeeded} selections be made but ${chosenIndices.length} were instead.`);
    }

    const result = chosenIndices.map(indexOfChosen =&gt; this.options[indexOfChosen]);
    if (result.some(item =&gt; item == null))
        throw new Error('Selection can not be null/undefined.');
    return result;
}