I am looking to get type inference based on the value of keys in an array.
For context, I'm working on a class where you can pass in a list of options and a default value:
interface Option {
value: string;
}
interface SelectArgs {
options: Option[];
defaultValue: Option['value'];
}
class SelectConfig {
options: Option[];
defaultValue: Option['value'] // can I get the value options with autocompletion here?
constructor(args: SelectArgs ) {
Object.assign(this, args);
}
}
I would be using this in the context of a class, so for example, if I instantiate a new class:
const select = new SelectOptions({
options: [{value: 'Hello'}, {value: 'World'}]
defaultValue: "Hello" // I want to give me type inference of either "Hello" | "World"
})
What is the best way to type defaultValue in this case so it is always one of the passed in values of options?