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
- Template Literal Type Basics
- Modeling Structured Strings
- String Unions and Autocomplete
- Intrinsic String Manipulation Types