0
0
Typescriptprogramming~20 mins

Pattern matching with template literals in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Template Literal Pattern Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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");
AID: 123
BNo match
CID: user_123
DSyntaxError
Attempts:
2 left
💡 Hint
Look at the regular expression and what part is captured.
Predict Output
intermediate
2: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");
AHandled event: ButtonClick
BUnknown event
CHandled event: onButtonClick
DTypeError
Attempts:
2 left
💡 Hint
Check the string passed and the conditions in the function.
🔧 Debug
advanced
2: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"));
AType 'string' is not assignable to type 'VersionString' without assertion
BRegex pattern is incorrect and does not match 'v2'
CFunction returns undefined instead of string
DSyntax error in template literal type
Attempts:
2 left
💡 Hint
Check the type assignment inside the if block.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in template literal pattern matching
Which option contains a syntax error in TypeScript template literal pattern matching?
Atype File = `${string}.txt`;
Btype Greeting = `Hello, ${string}`;
Ctype Path = `/home/${string}`;
Dtype Invalid = `${number`;
Attempts:
2 left
💡 Hint
Look carefully at the backticks and braces.
🚀 Application
expert
3: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"
];
A3
B4
C5
D2
Attempts:
2 left
💡 Hint
Check which strings start with 'ERR_', followed by a number, then an underscore and any string.