Here is the data model representation:
A person's daily data structure
interface FetchDocument {
Date : number,
Hours : number,
}
Grouping collection of these data points
type Data_Array = Set<FetchDocument> | FetchDocument[]
interface GroupDocument {
name : string,
Data : Data_Array,
}
Sample test data provided below
let fetchArray : GroupDocument[] = [
{
name : 'John',
Data : [
{
Date : 13,
'Hours' : 14
},
{
Date : 12,
'Hours' : 433
}
]
}
]
An attempt to find specific data using the `find` method:
for(let i= 0; i < fetchArray.length ; i++){
let obj = fetchArray[i].Data.find((obj : FetchDocument) => obj.Date === 13 )
}
The compiler presents an error message due to the use of Set within Data_Array.
Error TS2339: Property 'find' does not exist on type 'Data_Array'.
Property 'find' does not exist on type 'Set'.
Various attempts have been made to resolve this issue including narrowing and re-assigning:
Narrowing
if(Array.isArray(fetchArray[i].Data)){
let obj = fetchArray[i].Data.find((obj : FetchDocument) => obj.Date === 13 )
}
if(typeof(fetchArray[i].Data.find) === 'function'){
let obj = fetchArray[i].Data.find((obj : FetchDocument) => obj.Date === 13 )
}
if(fetchArray[i].Data instanceof Array){
let obj = fetchArray[i].Data.find((obj : FetchDocument) => obj.Date === 13 )
}
Re-assigning to array
fetchArray[i].Data = [...fetchArray[i].Data]
let obj = fetchArray[i].Data.find((obj : FetchDocument) => obj.Date === 13 )
fetchArray[i].Data = Array.from(fetchArray[i].Data.values())
let obj = fetchArray[i].Data.find((obj : FetchDocument) => obj.Date === 13 )
Despite these efforts, the error persists. Further solutions are sought to address this issue.
Playground link : Link
Set datatype usage aims at detecting duplicate entries, hence modifying it would necessitate adjustments in other sections of the codebase.