Challenge - 5 Problems
Template Literal Pattern Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of pattern matching with template literals
What is the output of this TypeScript code using pattern matching with template literals?
Typescript
const input = "user_123"; const match = input.match(/^user_(\d+)$/); console.log(match ? `ID: ${match[1]}` : "No match");
Attempts:
2 left
💡 Hint
Look at the regular expression and what part is captured.
✗ Incorrect
The regex /^user_(\d+)$/ matches strings starting with 'user_' followed by digits only. The digits are captured and printed as ID.
❓ Predict Output
intermediate2:00remaining
Extracting parts with template literal types
What will be the output of this TypeScript code using template literal types and pattern matching?
Typescript
type EventName = `on${string}Click`; function handleEvent(event: EventName) { if (event.startsWith("on") && event.endsWith("Click")) { console.log(`Handled event: ${event}`); } else { console.log("Unknown event"); } } handleEvent("onButtonClick");
Attempts:
2 left
💡 Hint
Check the string passed and the conditions in the function.
✗ Incorrect
The string 'onButtonClick' starts with 'on' and ends with 'Click', so it matches the condition and prints the handled event.
🔧 Debug
advanced2:30remaining
Why does this pattern matching fail?
This TypeScript code tries to extract a version number from a string using template literal types but fails. What is the cause?
Typescript
type VersionString = `v${number}`; function parseVersion(v: string) { if (v.match(/^v\d+$/)) { const version: VersionString = v; return version; } return "Invalid version"; } console.log(parseVersion("v2"));
Attempts:
2 left
💡 Hint
Check the type assignment inside the if block.
✗ Incorrect
Even though the string matches the pattern, TypeScript cannot guarantee the string type matches the template literal type without a type assertion.
📝 Syntax
advanced1:30remaining
Identify the syntax error in template literal pattern matching
Which option contains a syntax error in TypeScript template literal pattern matching?
Attempts:
2 left
💡 Hint
Look carefully at the backticks and braces.
✗ Incorrect
Option D has a missing closing brace '}' after 'number', causing a syntax error.
🚀 Application
expert3:00remaining
Count how many strings match this template literal type
Given the type `type Code = `ERR_${number}_${string}`;` and the array below, how many strings match the `Code` type pattern?
Typescript
type Code = `ERR_${number}_${string}`; const codes = [ "ERR_404_NotFound", "ERR_500_ServerError", "WARN_300_SlowResponse", "ERR_abc_Invalid", "ERR_123_", "ERR_0_ZeroError" ];
Attempts:
2 left
💡 Hint
Check which strings start with 'ERR_', followed by a number, then an underscore and any string.
✗ Incorrect
Strings matching the pattern must start with 'ERR_', then a number, then '_', then any string (including empty). 'ERR_abc_Invalid' fails because 'abc' is not a number. 'WARN_300_SlowResponse' fails prefix. 'ERR_123_' matches because empty string after underscore is allowed.