I have dual interfaces
that look like this
interface CatStateProps {
data: CatState[],
food: number[]
}
interface DogStateProps {
data: DogState[],
food: string[]
}
that I am utilizing in a type
declaration
type AnimalState= {
cat?: CatStateProps ,
dog?: DogStateProps,
};
Using this type
, I am constructing an object
const catsAndDogs: AnimalState = {
cat: {data: [...], food: [...]}
dog: {data: [...], food: [...]}
};
Now, my intention is to access the dog
property
let {dog} = catsAndDogs // dog is of type CatStateProps | DogStateProps
if(dog) {
let {data} = dog // data has the types CatState[] | DogState[]
foo(data);
}
However, when I pass data
as a parameter to the function foo()
, it ends up being of type any
within the actual function.
const foo = (data: ?) => {...}
So, how can I dynamically assign the type of data
to the function foo()
, without explicitly defining it like this
const foo = (data: CatState[] | DogState[]) => {...}
I am seeking a way to dynamically pass the type of data
into foo()
, so if I modify AnimalState
, I won't need to manually update the type within foo()
.