In my current project, I have a component named ComponentA which has a defined interface. Here is the snippet of the interface:
interface A1 {
isSingle: true;
value: number;
}
interface A2 {
isSingle: false;
value: [number, number];
}
export type IA = A1 | A2;
function ComponentA({ isSingle = false, value }: IA): void {
if (!isSingle) {
console.log(value[0]);
}
};
export default ComponentA;
When passing isSingle=false
, we should expect the value as an array with two numeric elements. However, when trying to access value[0]
, I encountered a TypeScript error:
Element implicitly has an 'any' type because expression of type '0' can't be used to index type 'number | [number, number]'. Property '0' does not exist on type 'number | [number, number]'.
I am currently stuck and unable to find a solution for this issue. Any help would be greatly appreciated.