0
0
Typescriptprogramming~10 mins

Building type-safe string patterns in Typescript - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a string literal type for colors.

Typescript
type Color = [1];
Drag options to blanks, or click blank then click option'
A"red" | "green" | "blue"
Bnumber
Cstring
Dboolean
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'string' instead of specific string literals.
Using number or boolean types instead of string literals.
2fill in blank
medium

Complete the code to create a type-safe pattern for a URL string starting with 'http'.

Typescript
type HttpUrl = `http${string}`;
Drag options to blanks, or click blank then click option'
A"http" | "https"
B"https"
Cstring
D"http"
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'string' alone inside the template literal.
Using a union of strings inside the template literal incorrectly.
3fill in blank
hard

Fix the error in the type that should match strings ending with '.json'.

Typescript
type JsonFile = `${string}.json`;
Drag options to blanks, or click blank then click option'
A"json"
B".txt"
C".json"
D".js"
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the dot before 'json'.
Using incorrect file extensions.
4fill in blank
hard

Fill both blanks to define a type-safe pattern for a version string like 'v1.0'.

Typescript
type Version = `${"v"}${string}${"."}${string}`;
Drag options to blanks, or click blank then click option'
A"v"
Bnumber
C"."
Dstring
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'string' or 'number' literals incorrectly in template literals.
Missing the dot between version numbers.
5fill in blank
hard

Fill all three blanks to create a type-safe pattern for a file path like '/home/user'.

Typescript
type FilePath = `${"/"}${"home"}${"/"}${"user"}`;
Drag options to blanks, or click blank then click option'
A"/"
Bstring
C"user"
D"home"
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up the order of folder names and slashes.
Using 'string' instead of specific literals.