Annotate parameters and results
Describe an object with a type and return a formatted string from a typed function.
Learn how TypeScript describes the shape of data. These three beginner exercises cover typed function parameters, an interface for array items and a small generic helper.
Know JavaScript functions, objects and arrays. The lessons introduce type annotations, interfaces and generic type parameters step by step.
Describe an object with a type and return a formatted string from a typed function.
Filter an array of User objects and return a string array of names.
Write first<T>() so that the result type follows the array you pass in.
Create a Ticket type and a describeTicket function that returns the passenger and seat separated by " — ".
Open lesson and hintsWrite activeNames(users: User[]): string[] so that it returns the names of the active users. Print the result joined by a comma and a space.
Open lesson and hintsWrite a generic function first<T>(items: T[]): T | undefined that returns the first item of an array, or undefined for an empty array. Print the result for a number array, a string array and an empty array.
Open lesson and hintsRead the types before you write the body. Predict the printed result, then run your attempt. Afterwards change an annotation on purpose and check the error in a full TypeScript project or editor.
The quick playground transpiles TypeScript and runs the JavaScript output. It does not report every static type error, so use a full TypeScript project to check complete type safety.
Completion is self-reported. There is no automated grading, and progress is saved only on this device.