Advice on how to type backend response data for a TypeScript newbie?
In my app I have a books table where each row has quite a few colums.
In the frontend, I have a Book type with all of those columns. I can't figure out how to deal with scenarios where I may want to map over the queried data inside Laravel, to exclude stuff like book typing, language, pages, etc just because most parts of the app don't actually need all of that, so that responses would be smaller and cleaner.
The problem is that then the type is not reliable - what if in the future I want to add some functionality that would leverage those other fields, and then bam, there's an error, which defeats the whole purpose of having TypeScript. What's the best practice here?
Is it making those "secondary" properties optional? Don't like the idea of that personally. Or is it making other types, like a BookImageCard type, which in my case only needs a cover link and a title?
Or is removing those values not neccessary in the first place?
If I understand you correctly you have a table with information about books, and you want to show parts of that information on one page and all the information on another page?
If so then the easiest would be to just
public function index()
{
$books = Book::select('author', 'title', 'released', 'format', 'genre')->paginate(25);
return view('books.index')
->with(['books' => $books]);
}
public function show(Book $book)
{
return view('books.show')->with(['book' => $book]);
}