The name property expects a string, but 100 is a number, causing a type error.
Step 2: Fix the type mismatch
Assign a string value to name, for example, 'Book', to match the type definition.
Final Answer:
Change name to a string value, e.g., 'Book'. -> Option A
Quick Check:
Property types must match definition [OK]
Hint: Match property types exactly as defined [OK]
Common Mistakes:
Assigning wrong types to properties
Removing type annotations unnecessarily
Adding unrelated properties without fixing errors
5. You want to design a type pattern for a list of users where each user has a name and an optional email. Which type design pattern is best to ensure clarity and maintainability?
hard
A. Use a union type: type User = { name: string } | { email: string }
B. Use an interface with optional email property: interface User { name: string; email?: string; }
C. Use a type alias with email as a required string: type User = { name: string; email: string; }
D. Use any type: type User = any;
Solution
Step 1: Understand the requirement for optional email
The email property should be optional, meaning it may or may not exist on a user object.
Step 2: Choose the pattern that supports optional properties clearly
Using an interface with email?: string marks email as optional and keeps the type clear and maintainable.
Final Answer:
Use an interface with optional email property: interface User { name: string; email?: string; } -> Option B
Quick Check:
Optional property syntax = ? [OK]
Hint: Use ? to mark optional properties in interfaces [OK]
Common Mistakes:
Making optional properties required
Using union types incorrectly for optional fields
Using any type which loses type safety
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.