Menu
Coddy logo textTech

Inline Object Type Annotations

Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 37 of 73.

Sometimes you need to define the structure of an object for a single, specific use case. Instead of creating a reusable type alias or interface, TypeScript allows you to define the object's shape directly where you declare the variable. This is called an inline object type annotation.

The syntax uses curly braces to describe the object's properties and their types right in the variable declaration:

let user: { name: string; id: number } = {
  name: "Alice",
  id: 123
};

This approach is perfect for one-off object structures that you don't plan to reuse elsewhere in your code.

challenge icon

Challenge

Easy

Create a variable named student with an inline object type annotation that defines the following structure:

  • name property of type string
  • studentId property of type number
  • isEnrolled property of type boolean

Assign the variable the following values:

  • name: "Sarah Johnson"
  • studentId: 12345
  • isEnrolled: true

Create another variable named course with an inline object type annotation that defines the following structure:

  • title property of type string
  • credits property of type number
  • instructor property of type string

Assign the variable the following values:

  • title: "Introduction to TypeScript"
  • credits: 3
  • instructor: "Dr. Smith"

Print the following information on separate lines:

  • The student's name
  • The student's ID
  • The course title
  • The number of credits

Try it yourself

// TODO: Write your code here
// Create the student variable with inline object type annotation
// Create the course variable with inline object type annotation
// Print the required information
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Introduction To TypeScript