📖 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
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
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
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
Hint
Use console.log(book); and console.log(isLongBook); to show the results.
Practice
(1/5)
1. What does inline object type annotation in TypeScript do? let user: { name: string; age: number };
easy
A. It defines the shape of the object directly where it is used.
B. It creates a new class named user.
C. It converts the object to a JSON string.
D. It declares a function that returns an object.
Solution
Step 1: Understand inline object type annotation
Inline object type annotation specifies the properties and their types directly in the variable declaration.
Step 2: Analyze the example
The example { name: string; age: number } tells TypeScript that user must have a string name and a number age.
Final Answer:
It defines the shape of the object directly where it is used. -> Option A
Quick Check:
Inline type annotation = direct object shape [OK]
Hint: Inline means type right where variable is declared [OK]
Common Mistakes:
Thinking it creates a class
Confusing with JSON conversion
Assuming it declares a function
2. Which of the following is the correct syntax for inline object type annotation in TypeScript?
easy
A. let data: (id: number, title: string) = { id: 1, title: 'Book' };
B. let data = { id: number, title: string } = { id: 1, title: 'Book' };
C. let data: { id: number; title: string } = { id: 1, title: 'Book' };
D. let data: [number, string] = { id: 1, title: 'Book' };
Solution
Step 1: Identify correct inline object type syntax
Inline object type annotation uses curly braces with property names and types, like { id: number; title: string }.
Step 2: Check each option
let data: { id: number; title: string } = { id: 1, title: 'Book' }; correctly uses this syntax and assigns a matching object. Options A, B, and D use incorrect syntax or types.
Final Answer:
let data: { id: number; title: string } = { id: 1, title: 'Book' }; -> Option C
Quick Check:
Curly braces with property types = correct syntax [OK]
Hint: Use curly braces with property: type pairs [OK]
Common Mistakes:
Using parentheses instead of braces
Assigning types inside object literal
Confusing arrays with objects
3. What will be the output of this TypeScript code?
function printUser(user: { name: string; age: number }) {
console.log(`${user.name} is ${user.age} years old.`);
}
printUser({ name: 'Alice', age: 30 });
medium
A. Alice is 30 years old.
B. Error: Missing property age
C. Alice is undefined years old.
D. Error: Type mismatch in function argument
Solution
Step 1: Understand the function parameter type
The function expects an object with name as string and age as number.
Step 2: Check the argument passed
The argument { name: 'Alice', age: 30 } matches the expected type exactly.
Final Answer:
Alice is 30 years old. -> Option A
Quick Check:
Matching object type = correct output [OK]
Hint: Match object shape exactly to avoid errors [OK]
Common Mistakes:
Passing missing or extra properties
Using wrong types for properties
Ignoring inline type annotation
4. Identify the error in this TypeScript code snippet:
B. Extra property 'price' not allowed in inline type annotation.
C. Property 'id' should be a string, not number.
D. Object must be assigned to a function, not a variable.
Solution
Step 1: Check the declared type
The type annotation allows only id and name properties.
Step 2: Compare with assigned object
The object has an extra property price which is not declared in the type, causing an error.
Final Answer:
Extra property 'price' not allowed in inline type annotation. -> Option B
Quick Check:
Extra properties cause errors in inline types [OK]
Hint: Only use declared properties in inline object types [OK]
Common Mistakes:
Adding extra properties not in type
Ignoring TypeScript error messages
Confusing semicolon placement
5. You want to write a function that accepts an object with a string title and an optional number year. Which inline type annotation is correct?
hard
A. function showBook(book: { title: string; year: number | undefined }) { console.log(book.title); }
B. function showBook(book: { title: string | number; year: number }) { console.log(book.title); }
C. function showBook(book: { title?: string; year: number }) { console.log(book.title); }
D. function showBook(book: { title: string; year?: number }) { console.log(book.title); }
Solution
Step 1: Understand optional properties in inline types
Optional properties are marked with a question mark ? after the property name.
Step 2: Analyze each option
function showBook(book: { title: string; year?: number }) { console.log(book.title); } correctly marks year as optional with year?: number. Other options either make title optional or use incorrect types.
Final Answer:
function showBook(book: { title: string; year?: number }) { console.log(book.title); } -> Option D
Quick Check:
Optional property uses ? after name [OK]
Hint: Use ? after property name for optional fields [OK]
Common Mistakes:
Making required properties optional
Using union types incorrectly
Confusing optional with undefined type
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.