Bird
0
0

You have an interface Person with optional property middleName?. How would you correctly declare it?

hard📝 Application Q8 of 15
Angular - TypeScript in Angular
You have an interface Person with optional property middleName?. How would you correctly declare it?

Choose the correct interface declaration:
Ainterface Person { firstName: string; middleName?: string; lastName: string; }
Binterface Person { firstName: string; middleName: string; lastName: string; }
Cinterface Person { firstName: string; middleName: optional string; lastName: string; }
Dinterface Person { firstName: string; middleName: string | undefined; lastName: string; }
Step-by-Step Solution
Solution:
  1. Step 1: Understand optional properties syntax

    Optional properties use a question mark after the name, like middleName?: string;.
  2. Step 2: Compare options

    interface Person { firstName: string; middleName?: string; lastName: string; } correctly uses the question mark. interface Person { firstName: string; middleName: string; lastName: string; } makes it required. interface Person { firstName: string; middleName: optional string; lastName: string; } uses invalid syntax. interface Person { firstName: string; middleName: string | undefined; lastName: string; } uses a union but is not the standard optional syntax.
  3. Final Answer:

    interface Person { firstName: string; middleName?: string; lastName: string; } -> Option A
  4. Quick Check:

    Optional property syntax = question mark [OK]
Quick Trick: Use '?' after property name to mark it optional [OK]
Common Mistakes:
  • Omitting '?' for optional properties
  • Using invalid keywords like 'optional'
  • Using union types instead of '?'

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes