The objective is to develop a reusable method for filtering out items from an array that implements one of two interfaces
Providing a code example would be most helpful:
interface IDuration {
start: number;
end: number;
}
interface IRelativeDuration {
relativeStart: number;
relativeEnd: number;
}
export const enforceBoundries = (
point: number,
items: Array<IDuration> | Array<IRelativeDuration>,
): void => {
let startKey: string
let endKey: string
// **HAVING TROUBLE WITH THIS**
if(/* iDuration */) {
startKey = 'start'
endKey = 'end'
} else {
startKey = 'relativeStart'
endKey = 'relativeEnd'
}
items.forEach(item => {
if(item[startKey] > point) {
//....
}
})
}
The solution might lie in Generic Constraints, but TypeScript is not my strong suit
I have tried various conditions in the if
statement but can't find one that successfully compiles the application