Practice & Assessment
Test your understanding of Classes and Object-Oriented TypeScript
Multiple Choice Questions
5What is the difference between private and protected in TypeScript?
When must a subclass call super() in its constructor?
What is the purpose of a private constructor in the Singleton pattern?
What TypeScript config option must be enabled to use decorators?
What is parameter property shorthand in a TypeScript constructor?
Coding Challenges
1Build a typed observable value
Create a class Observable<T> that wraps a value. Implement a private backing value, a get value() getter, and a set value(v) setter that notifies subscribers. Add subscribe(listener: (value: T) => void): () => void that registers a listener and returns an unsubscribe function. Unsubscribing should stop future notifications. Write a demo: create an Observable<number> starting at 0, subscribe two listeners, set value to 5 (both notified), unsubscribe one, set to 10 (one notified). Estimated time: 20 minutes.
Mini Project
Task Queue with Decorators
Build a typed task queue system. Define a Task interface with id (string), type (string), payload (unknown), and status ('pending' | 'running' | 'done' | 'failed'). Create an abstract TaskProcessor<T> class with a protected abstract process(payload: T): Promise<void> method and a public run(task: Task): Promise<void> that sets status, calls process, and handles errors. Implement two concrete processors: EmailProcessor (payload: { to: string; subject: string }) and NotificationProcessor (payload: { userId: string; message: string }) that log formatted output. Add a @Log method decorator to run() that logs the task id and type at start and end. Create a TaskQueue class with enqueue(task) and processNext(): Promise<void> that uses a Map of task types to processors. Demonstrate enqueuing and processing five mixed tasks.
