I am trying to apply TypeScript in my ReactJS project. I am starting apply it to one of my components but somehow I am having hard time applying typescript when a components accepts object with properties as its props.
Here is my code
interface iValue {
results: string[] | number[];
}
interface DropDownInputInt {
name: string;
value: iValue | string;
multiple: boolean;
}
const DropDownInput = ({
name,
value,
multiple
}: DropDownInputInt) => {
return (
<>
<Dropdown
name={name}
value={multiple && value
? value.hasOwnProperty('results')
? value.results
: value
: value
}
/>
</>
)
})
For my props named "value" I am accepting 2 possible inputs, string or an object(with 'results' key and can have both an array string or number)
See image below for a sample of props.value that has a string value.
Not sure why but currently my VSCode is showing errors when I tried to use value.results. See image below for the error.
UPDATE
I tried to incopmperate some answers from
interface iValue {
results: (string | number | boolean)[];
}
interface DropDownInputInt {
name: string;
value: iValue | boolean | number | string;
multiple: boolean;
}
const DropDownInput = ({
name,
value,
multiple
}: DropDownInputInt) => {
return (
<>
<Dropdown
name={name}
value={
multiple && value
? typeof value === 'string' ||
typeof value === 'number' ||
typeof value === 'boolean'
? value
: value.results
: typeof value === 'object' && value
}
/>
</>
)
})
But I am still getting errors, now its pointing to value property in my Dropdown
But when I check the property that being accepted by the "Dropdown" component
Thanks in advance




