0
0
Typescriptprogramming~20 mins

Template literal with unions in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Template Literal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of template literal with union types
What is the output of the following TypeScript code when compiled to JavaScript and run?
Typescript
type Color = "red" | "blue";
type Size = "small" | "large";

function describe(color: Color, size: Size): string {
  return `${color}-${size}`;
}

console.log(describe("red", "large"));
ATypeError at runtime
B"red large"
C"redlarge"
D"red-large"
Attempts:
2 left
💡 Hint
Look at how template literals combine strings and variables.
🧠 Conceptual
intermediate
2:00remaining
Possible values of a template literal union type
Given the following TypeScript type, how many possible string values does the type `Message` represent?
Typescript
type Status = "success" | "error";
type Action = "save" | "delete";
type Message = `${Status}_${Action}`;
A4
B2
C3
D1
Attempts:
2 left
💡 Hint
Count all combinations of Status and Action joined by an underscore.
🔧 Debug
advanced
2:00remaining
Why does this template literal union type cause an error?
Consider this TypeScript code snippet. Why does it cause a type error?
Typescript
type A = "x" | "y";
type B = "1" | "2";
type C = `${A | B}`;

const val: C = "x";
AThe variable val is assigned a value not in type C
BThe type C includes values not assignable to "x"
CThe union inside the template literal is invalid syntax
DTemplate literals cannot use union types
Attempts:
2 left
💡 Hint
Check what values type C actually includes.
Predict Output
advanced
2:00remaining
Output of nested template literal with unions
What is the output of this TypeScript code when compiled and run as JavaScript?
Typescript
type Prefix = "get" | "set";
type Property = "Name" | "Age";

function makeKey(prefix: Prefix, prop: Property): string {
  return `${prefix}${prop}`;
}

console.log(makeKey("set", "Age"));
A"setAge"
B"set Age"
C"set-Age"
DCompilation error
Attempts:
2 left
💡 Hint
Look at how the template literal joins prefix and prop without spaces.
📝 Syntax
expert
3:00remaining
Which option correctly defines a template literal union type for CSS classes?
You want to define a TypeScript type for CSS classes that combine a color and a size with a dash, like "red-small" or "blue-large". Which option correctly defines this type?
A
type Color = "red" | "blue";
type Size = "small" | "large";
type CssClass = `${Color & Size}`;
B
type Color = "red" | "blue";
type Size = "small" | "large";
type CssClass = `${Color | Size}`;
C
type Color = "red" | "blue";
type Size = "small" | "large";
type CssClass = `${Color}-${Size}`;
D
type Color = "red" | "blue";
type Size = "small" | "large";
type CssClass = `${Color}${Size}`;
Attempts:
2 left
💡 Hint
Use a template literal with both types separated by a dash.