There's a difference between any
and unknown
.
In short for your case - you can't treat unknown
as any
thing, or as Book
like you did in your code.
If your method returned Promise<any>
or Promise<Book>
you'd be able to do what you attempted here.
I would go with the latter and annotate the getSingleBook method as such:
getSingleBook(id: number): Promise<Book> {
//...
const potentialBook = data.val();
const book: Book = validateBook(potentialBook); // throw an error if not a book
resolve(book);
//...
}
You can just assert the resolved value as a book if you're 100% sure it is and always will be, but that's ill advised:
getSingleBook(id: number): Promise<Book> {
//...
resolve(book as Book);
//...
}
Thanks jonrsharpe.