Type of Observable?

Viewed 51

How to write the correct type instead of any in finction getAll()

getAll(): Observable<any> {
return this.http
  .get<{
    results: CharacterData[];
    info: CharacterInfo;
  }>(characterUrl)
  .pipe(map((el) => el));
}
1 Answers

You can create an interface of the correct type, then use that instead of any. Same with your .get<...> or stick with the method you're currently doing.

Interface:

interface CorrectType {
    results: CharacterData[];
    info: CharacterInfo;
}

Using Interface:

getAll(): Observable<CorrectType> {
    return this.http.get<CorrectType>(characterUrl).pipe(map((el) => el));
}

Using original method:

getAll(): Observable<{
        results: CharacterData[];
        info: CharacterInfo;
    }> {
    return this.http
        .get<{
            results: CharacterData[];
            info: CharacterInfo;
        }>(characterUrl)
        .pipe(map((el) => el));
}
Related