I have a method where the type can be an array, but I need to verify that it is not an array before accessing the argument.
However, despite my check, I am still encountering the following error (excerpt) on line this.setState({ cuisine });
:
The type 'readonly SelectOption[]' does not contain the properties 'value' and 'label' as required by type 'SelectOption'
When I hover over cuisine
in the error message, VS Code indicates that its type is
SelectOption | readonly SelectOption[]
Here is the sample code snippet:
interface SelectOption {
value: number | string;
label: string
}
private onCuisineChange(cuisine: ValueType<SelectOption>): void {
if (Array.isArray(cuisine) && cuisine.length > 1) {
throw new Error(`Expected cuisine to be an object, but got ${cuisine}`);
}
if (cuisine) {
this.setState({ cuisine });
} else {
}
}
This is using TypeScript version 3.5.3