0
0
Typescriptprogramming~15 mins

Object type annotation inline in Typescript - Mini Project: Build & Apply

Choose your learning style9 modes available
Object type annotation inline
📖 Scenario: You are creating a simple program to store information about a book in a library system.
🎯 Goal: Learn how to write an inline object type annotation in TypeScript to describe the shape of a book object.
📋 What You'll Learn
Create an object variable with inline type annotation
Include properties: title (string), author (string), and pages (number)
Assign exact values to the object properties
Print the book object to the console
💡 Why This Matters
🌍 Real World
Inline object type annotations help you quickly describe the shape of data objects without creating separate interfaces or types. This is useful when you want to keep your code simple and clear for small objects.
💼 Career
Many TypeScript jobs require you to write clear and precise type annotations to avoid bugs and improve code readability. Inline object types are a common way to do this in everyday coding.
Progress0 / 4 steps
1
Create a book object with inline type annotation
Create a variable called book with an inline object type annotation that has these exact properties: title as a string, author as a string, and pages as a number. Assign the values "The Hobbit" for title, "J.R.R. Tolkien" for author, and 310 for pages.
Typescript
Need a hint?

Use const book: { title: string; author: string; pages: number } to declare the variable with inline type annotation.

2
Add a variable for minimum pages
Create a variable called minPages and set it to the number 100.
Typescript
Need a hint?

Just write const minPages = 100; to create the variable.

3
Check if the book has enough pages
Create a variable called isLongBook and set it to true if book.pages is greater than minPages, otherwise false. Use a simple comparison expression.
Typescript
Need a hint?

Use const isLongBook = book.pages > minPages; to compare the pages.

4
Print the book object and length check
Write two console.log statements: one to print the book object, and one to print the value of isLongBook.
Typescript
Need a hint?

Use console.log(book); and console.log(isLongBook); to show the results.