← All Exercises

Template Literal Types

basictypescript~15min

Background

Template literal types enable type-safe string patterns, useful for API routes, event names, and configuration keys.

Problem

Write a TypeScript type that represents API endpoint strings in the format "METHOD /path" using template literal types, and a function that validates endpoints.

Input

A string representing an HTTP method and a string representing a path.

Output

A typed string in the format "METHOD /path" or a type error.

Sample tests

Sample 1

Input: "GET", "/users"
Expected: "GET /users"

Your solution

Learning objective

Learn template literal types for creating type-safe string patterns.