I am trying to create a generic option type/interface which will be used to create specific option types. This generic type/interface takes a set of options as input. The parameter value contains the current selection, and options contains all the options. The parameter value should only be allowed to hold strings contained in options and options should be defined from the list of options defined externally.
This is my current progress:
export type GenericListOfOptions<list> = {
type: 'list',
name: string,
value: list,
options: Array<list>, // I am not sure if this is right...
}
// Here i define my options:
const these_options = ['yes', 'no', 'maybe'] as const;
// Here i define a specific option type from the
// generic option type. This specific option type is
// defined from the 'these_options' constant.
type MySpecificOptions = GenericListOfOptions<typeof these_options[number]>
// Here i use the type to create an option object.
let my_options: MySpecificOptions = {
type: 'list',
name: 'am i going to the gym?',
value: 'no',
options: these_options, // Here is an error relating readonly properties...
// 'options' should be equal to ['yes', 'no', 'maybe'].
// I preferrably want to reuse the 'these_options' array to define the 'options' parameter.
}
The error given is:
The type 'readonly ["yes", "no", "maybe"]' is 'readonly' and cannot be assigned to the mutable type '("yes" | "no" | "maybe")[]'.ts(4104)
interfaces.ts(137, 5): The expected type comes from property 'options' which is declared here on type 'MySpecificOptions '
But maybe there are other more general errors of this idea, so any help would be greatly appreciated!