Imagine having two data types, both of them have an id property:
type Foo = {
id: string,
// other props
}
type Bar = {
id: number,
// other props
}
Now imagine a Table component in React which accepts an array of data as a property. This component needs to be generic, but we know that it requires an id prop for every data object in the data array. This id can either be a number or a string. The props might be typed as follows:
type Props = {
data: Array<{id: number | string}>
}
The problem is that the above notation does not work. Is there a way to be able to 'retype' certain properties?
I have looked into generic types but I can't seem to find a solution without changing the Foo and Bar type definitions.
type U = Foo | Barand thenArray<U>? - user6445533