After assigning the getEvent()
type to IEvent
, I encountered the following error message:
TS2322: Type 'IEvent | undefined' is not assignable to type 'IEvent'.
import { Injectable } from "@angular/core"
import { Subject, Observable } from "rxjs"
import { IEvent } from "./event.model"
@Injectable()
export class EventService {
getEvents():Observable<IEvent[]> {
let subject = new Subject<IEvent[]>()
setTimeout(() => { subject.next(EVENTS); subject.complete() }, 200)
return subject
}
getEvent(id: number): IEvent {
return EVENTS.find(event => event.id === id) // This is where the error occurred
}
}