Hint: Match full pattern including suffix exactly [OK]
Common Mistakes:
Ignoring required suffix in pattern
Thinking '@' is invalid in template literals
Confusing type errors with runtime errors
5. You want to create a type-safe pattern for a version string like v1.2.3 where each number can be one or more digits. Which of these TypeScript types correctly enforces this pattern?
hard
A. type Version = `v${number}.${number}.${number}`;
B. type Version = `v${string}.${string}.${string}`;
C. type Version = `v${number}.${string}.${number}`;
D. type Version = `v${number}.${number}`;
Solution
Step 1: Understand the version pattern
The version string starts with 'v' followed by three numbers separated by dots.
Step 2: Evaluate each type
A uses number for all three parts, matching the pattern exactly. B uses string, allowing invalid formats. C mixes number and string incorrectly. D misses the third number part.
Final Answer:
type Version = `v${number}.${number}.${number}`; -> Option A
Quick Check:
All parts as number with dots = type Version = `v${number}.${number}.${number}`; [OK]
Hint: Use ${number} for numeric parts in template literals [OK]
Common Mistakes:
Using string instead of number for numeric parts
Missing parts of the pattern
Mixing types inconsistently
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.