I'm trying to have an interface with a conditional statement.
In an interface, I know we could have something like this below to have the conditional.
interface exampleInterface {
a: 'A' | 'B'
}
This way, we could ensure the input 'a' must be either 'A' or 'B'. I would like to populate this kind of conditional from an array like ['A', 'B']
Is there a way we can do like this below?
const a_list = Object.keys(config.a_list) // ['A', 'B'] I want to generate this from the config file.
interface exampleInterface {
a: oneOf(a_list)
}
The reason why I would like to this way is the input array is dynamic(getting information from a config file). Any idea or suggestions helps. Thank you.
Additional Information: I have created the array from the config file and did the below in the interface.
interface myInterface {
a: typeof a_list[number],
}
But when I tried to get the information from the config file from the user input like this below, I get the following error.
export default function myFunc(props: myInterface)
const [a] = useState(config.a_config[props.a].map((a) => a))
// Element implicitly has an 'any' type because the expression of type 'string' can't be used to index type.
// No index signature with a parameter of type 'string' was found on the type.