Argument of type '(book: Book) => void' is not assignable to parameter of type '(value: unknown) => void | PromiseLike'. Types of parameters 'book' and 'value' are incompatible. Type 'unknown' is not assignable to type 'Book'.
0
votes
1 Answers
-1
votes
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.
this.booksServices.getSingleBook
(which you haven't shown) returns aPromise<unknown>
, so there's no guarantee you'll get aBook
back. Please give a minimal reproducible example as text. – jonrsharpe