0Pricing
TypeScript Academy · Lesson

Template Literal Type Basics

Build string types from unions and placeholders.

Strings at the Type Level

Template literal types build new string literal types by combining other string types. They use the same backtick-and-placeholder syntax as runtime template strings, but operate purely on types.

A Simple Template Literal Type

Imagine a type alias Greeting defined as a template literal containing the text Hello, followed by a placeholder for the type Name. If Name is the literal 'Ada', then Greeting is the literal type 'Hello, Ada'.

type Name = "Ada"
// type Greeting = template literal: Hello, then Name
// Greeting is the string literal type "Hello, Ada"
const g: "Hello, Ada" = "Hello, Ada"
console.log(g)

All lessons in this course

  1. Template Literal Type Basics
  2. Modeling Structured Strings
  3. String Unions and Autocomplete
  4. Intrinsic String Manipulation Types
← Back to TypeScript Academy