Bird
Raised Fist0
Typescriptprogramming~10 mins

Object type annotation inline in Typescript - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Object type annotation inline
Start
↓
Declare variable with inline object type
↓
Assign object matching type
↓
Use variable
↓
End
This flow shows how to declare a variable with an inline object type, assign a matching object, and then use it.
Execution Sample
Typescript
const person: { name: string; age: number } = { name: "Alice", age: 30 };
console.log(person.name);
console.log(person.age);
Declares a variable 'person' with an inline object type and prints its properties.
Execution Table
StepActionEvaluationResult
1Declare 'person' with inline type {name: string; age: number}TypeScript checks typeVariable 'person' ready to hold matching object
2Assign {name: "Alice", age: 30} to 'person'Check object matches typeAssignment successful
3Access 'person.name'Retrieve value"Alice"
4Access 'person.age'Retrieve value30
5End of codeNo more actionsProgram ends
💡 All steps completed, program ends normally
Variable Tracker
VariableStartAfter AssignmentFinal
personundefined{name: "Alice", age: 30}{name: "Alice", age: 30}
Key Moments - 3 Insights
Why do we write the object type inline instead of using an interface?
Inline object types let you quickly specify the shape of an object right where you declare it, as shown in step 1 of the execution_table, without needing a separate interface.
What happens if the assigned object does not match the inline type?
TypeScript will show an error during the assignment step (step 2 in execution_table) because the object does not fit the declared inline type.
Can we access properties not declared in the inline type?
No, TypeScript will prevent accessing properties not declared in the inline type, ensuring safety as seen in steps 3 and 4 where only 'name' and 'age' are accessed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'person.name' at step 3?
Aundefined
B"Bob"
C"Alice"
D30
💡 Hint
Check the 'Result' column at step 3 in the execution_table
At which step does TypeScript check if the assigned object matches the inline type?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Action' and 'Evaluation' columns in the execution_table for the assignment step
If we add a property 'height: number' to the object but not in the inline type, what happens?
ANo error, property is accepted
BTypeScript error at assignment
CProperty is ignored silently
DError when accessing 'height'
💡 Hint
Refer to key_moments about type mismatch during assignment (step 2 in execution_table)
Concept Snapshot
Inline object type annotation syntax:
const varName: { prop1: type1; prop2: type2 } = { prop1: value1, prop2: value2 };
- Defines object shape directly where variable is declared
- TypeScript checks assigned object matches this shape
- Access properties safely with autocomplete and type checks
Full Transcript
This example shows how to declare a variable with an inline object type in TypeScript. First, the variable 'person' is declared with a type specifying it must have a 'name' string and an 'age' number. Then, an object matching this shape is assigned. Accessing 'person.name' and 'person.age' retrieves the values safely. TypeScript checks the object matches the inline type during assignment, preventing errors. Inline object types are useful for quick, local type definitions without creating separate interfaces.

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

  1. Step 1: Understand inline object type annotation

    Inline object type annotation specifies the properties and their types directly in the variable declaration.
  2. Step 2: Analyze the example

    The example { name: string; age: number } tells TypeScript that user must have a string name and a number age.
  3. Final Answer:

    It defines the shape of the object directly where it is used. -> Option A
  4. 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

  1. 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 }.
  2. 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.
  3. Final Answer:

    let data: { id: number; title: string } = { id: 1, title: 'Book' }; -> Option C
  4. 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

  1. Step 1: Understand the function parameter type

    The function expects an object with name as string and age as number.
  2. Step 2: Check the argument passed

    The argument { name: 'Alice', age: 30 } matches the expected type exactly.
  3. Final Answer:

    Alice is 30 years old. -> Option A
  4. 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:
let product: { id: number; name: string } = { id: 101, name: 'Pen', price: 5 };
medium
A. Missing semicolon after object declaration.
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

  1. Step 1: Check the declared type

    The type annotation allows only id and name properties.
  2. Step 2: Compare with assigned object

    The object has an extra property price which is not declared in the type, causing an error.
  3. Final Answer:

    Extra property 'price' not allowed in inline type annotation. -> Option B
  4. 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

  1. Step 1: Understand optional properties in inline types

    Optional properties are marked with a question mark ? after the property name.
  2. 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.
  3. Final Answer:

    function showBook(book: { title: string; year?: number }) { console.log(book.title); } -> Option D
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard