I’m stuck with this one TypeScript: Park Service Volunteer Program
import {
RaccoonMeadowsVolunteers,
RaccoonMeadowsActivity,
raccoonMeadowsVolunteers,
} from './raccoon-meadows-log';
import {
WolfPointVolunteers,
WolfPointActivity,
wolfPointVolunteers,
} from './wolf-point-log';
type CombinedActivity = RaccoonMeadowsActivity | WolfPointActivity;
type Volunteers = {
id: number;
name: string;
activities: CombinedActivity[];
};
function combineVolunteers(
volunteers: (RaccoonMeadowsVolunteers | WolfPointVolunteers)[]
) {
return volunteers.map( volunteer => {
let id
id = volunteer.id
if( typeof id === 'string'){
id = parseInt(id, 10)
}
return {
id: id,
name: volunteer.name,
activity: volunteer.activities
}
})
}
function isVerified( verified: string | boolean ){
if( typeof verified === 'string'){
return verified === 'Yes' ? true : false
}
return verified
}
function getHours(activity: CombinedActivity){
if('hours' in activity){
return activity.hours
}else{
return activity.time
}
}
function calculateHours(volunteers: Volunteers[]) {
return volunteers.map((volunteer) => {
let hours = 0;
volunteer.activities.forEach((activity) => {
if (isVerified( activity.verified )){
hours += getHours( activity )
}
});
return {
id: volunteer.id,
name: volunteer.name,
hours: hours,
};
});
}
const combinedVolunteers = combineVolunteers(
[].concat(wolfPointVolunteers, raccoonMeadowsVolunteers)
);
console.log( combinedVolunteers )
const result = calculateHours( combinedVolunteers )
console.log( result )
I get the error on the second last line of my code:
index.ts:76:32 - error TS2345: Argument of type '{ id: any; name: string; activity: RaccoonMeadowsActivity[] | WolfPointActivity[]; }[]' is not assignable to parameter of type 'Volunteers[]'.
Property 'activities' is missing in type '{ id: any; name: string; activity: RaccoonMeadowsActivity[] | WolfPointActivity[]; }' but required in type 'Volunteers'.
76 const result = calculateHours( combinedVolunteers )
~~~~~~~~~~~~~~~~~~
index.ts:18:3
18 activities: CombinedActivity[];
~~~~~~~~~~
'activities' is declared here.
Found 1 error in index.ts:76
What’s wrong? Why don’t the two types of Volunteers not match?