Búsqueda de sitios web

angular - Tipo 'Observable' no es asignable al tipo 'Observable'


Estoy recibiendo el siguiente error cuando intento obtener los elementos:

Type 'Observable<unknown[]>' is not assignable to type 'Observable<Event[]>'.
Type 'unknown[]' is not assignable to type 'Event[]'.
Type '{}' is missing the following properties from type 'Event': code, name, password, pollCat

events.service.ts:

import { Injectable } from '@angular/core';
import { AngularFirestore, AngularFirestoreCollection, AngularFirestoreDocument} from 'angularfire2/firestore';
import { Event } from '../models/events'
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class EventsService {

  eventsCollection : AngularFirestoreCollection<Event>;
  events : Observable<Event[]>;

  constructor(public afs: AngularFirestore) { 
    this.events = this.afs.collection('Events').valueChanges();
  }

  getEvents()
  {
    return this.events;
  }
}

events.ts:

export interface Event{

    code: string;
    name: string;
    password: string;
    pollCat : string;

  }

Respuesta #1

Necesitas proporcionar el tipo genérico al llamar al método collection, en lugar de usar el valor predeterminado unknown:

this.events = this.afs.collection<Event>('Events').valueChanges();
                              // ^ here

Puedes leer más sobre genéricos en el manual de TypeScript aquí.