Never type the word any
.
Obviously, that’s a slight exaggeration, but I do mean slight. Anywhere you can possibly give something a proper type, whether it be a controller property, a method argument, or a method return value, do so. It may seem tempting to just say “let’s just shut the compiler up for now” or “I’ll get around to it later”, but it even makes a huge difference when somebody else is reading your code for the first time, such as when you post questions here.
Naming conventions are also important, though less so. There are two problems with searchInter
as an interface name: it needs to start with a capital letter, because classes and interfaces should be PascalCase - this way they don’t collide with variables and method names in camelCase. Also, Inter
(or the Ixxx
I often see from folks steeped in C#) isn’t useful. Since all the names of things I see here are so generic (search
, item
, result
), I don’t know what you’re actually searching for. I’m going to pretend it’s books for now.
export class ResultsListPage {
results: Book[] = [];
constructor(private searchService: SearchService) {
searchService.getResults().pipe(untilDestroyed(this))
.subscribe(results => this.results = results);
}
}
untilDestroyed
comes from this project and is needed here to prevent leaking subscriptions. There are obviously other ways of doing this, such as keeping a Subscription
property in the class or using the AsyncPipe
, but untilDestroyed
is my favorite.