Bird
Raised Fist0
Typescriptprogramming~5 mins

Object type annotation inline in Typescript - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is an inline object type annotation in TypeScript?
It is a way to specify the shape of an object directly where it is used, without creating a separate interface or type alias.
Click to reveal answer
beginner
How do you write an inline object type annotation for a function parameter that expects an object with a string 'name' and a number 'age'?
You write it like this: <pre>function greet(person: { name: string; age: number }) { ... }</pre>
Click to reveal answer
intermediate
Why use inline object type annotations instead of interfaces?
Inline annotations are quick and simple for small or one-time use objects, avoiding extra code for interfaces when not needed.
Click to reveal answer
intermediate
Can inline object type annotations include optional properties? How?
Yes, by adding a question mark after the property name. Example:
{ name: string; age?: number }
means 'age' is optional.
Click to reveal answer
beginner
What happens if you pass an object missing a required property in an inline type annotation?
TypeScript will show an error because the object does not match the expected shape defined inline.
Click to reveal answer
How do you annotate a function parameter inline to accept an object with a 'title' string and 'pages' number?
Afunction read(book: { title: string; pages: number }) {}
Bfunction read(book: [string, number]) {}
Cfunction read(book: string, number) {}
Dfunction read(book: any) {}
Which syntax shows an optional property 'color' in an inline object type?
A{ color?: string }
B{ color: string? }
C{ ?color: string }
D{ color: optional string }
What is the benefit of inline object type annotations?
AThey make code run faster.
BThey automatically generate documentation.
CThey allow quick type definitions without creating separate interfaces.
DThey replace classes.
If a function expects parameter: { id: number; name: string }, what happens if you pass { id: 1 }?
AThe 'name' is assumed to be undefined.
BNo error, it works fine.
CRuntime error only.
DTypeScript error: missing 'name' property.
Which is a correct inline object type annotation for a variable holding a point with x and y as numbers?
Alet point: object = { x: 0, y: 0 };
Blet point: { x: number; y: number } = { x: 0, y: 0 };
Clet point: [number, number] = { x: 0, y: 0 };
Dlet point: (x: number, y: number) = { x: 0, y: 0 };
Explain how to use inline object type annotation for a function parameter in TypeScript.
Think about writing the type directly inside the function parameter parentheses.
You got /3 concepts.
    Describe how optional properties are declared in inline object type annotations and why they are useful.
    Optional means the property may or may not be present.
    You got /3 concepts.

      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