When I assign getEvent()
type to IEvent
, I get this error,
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) // Error here
}
}
CodePudding user response:
Add 'undefined' to your return type:
getEvent(id: number): IEvent | undefined {
return EVENTS.find(event => event.id === id) // Error here
}
CodePudding user response:
Define the IEvent type as "any". If that doesn't fix your issue try upgrading your rxjs version. This should fix your problem!