Practice & Assessment
Test your understanding of Object Types and Interfaces
Multiple Choice Questions
5Which feature is available in interface but NOT in type alias?
What does the & operator produce in TypeScript?
When does excess property checking apply?
What is the purpose of implements in a class declaration?
What type does Record<'admin' | 'user', boolean> produce?
Coding Challenges
1Model a blog post system with interfaces
Create three interfaces: Author with id (number), name (string), and optional bio (string). Post with id (number), title (string), content (string), author (Author), tags (string array), and publishedAt (Date or null). PublishedPost that extends Post and requires publishedAt as Date (not null). Write a function isPublished(post: Post): post is PublishedPost that returns true if publishedAt is not null. Create two sample Post objects โ one published, one draft โ and use the type guard to log different messages. Estimated time: 20 minutes.
Mini Project
Library Catalog System
Build a typed library catalog module. Define interfaces for: Genre (a string union type: 'fiction' | 'non-fiction' | 'science' | 'history'), Book (id number, title string, author string, genre Genre, yearPublished number, available boolean), and Library (name string, books Book array, Record of genre to count called genreCounts). Implement four functions: addBook(library, book) returns updated Library, checkOut(library, bookId) returns updated Library or throws if not available, getByGenre(library, genre) returns Book array, and buildGenreCounts(library) returns the genre count Record. Compose everything into a working demo that adds five books and performs checkouts. Zero compile errors with strict mode.
