Template Literals with Primitives
What you'll learn: How TypeScript automatically converts number, boolean, bigint, null, and undefined into string representations when used in template literal types.
The String Conversion Rule
Template literal types don't just work with string literal unions—they also accept primitive types. When you use number, boolean, bigint, null, or undefined in a template literal type, TypeScript automatically converts them to their string equivalents.
Think of it like JavaScript's template literal runtime behavior, but at the type level. Just as `Count: ${42}` produces "Count: 42" at runtime, the type `Count: ${number}` represents all possible strings that start with "Count: " followed by any number's string representation.
How Each Primitive Converts
number → Represents any numeric string:
`Value: ${number}`matches"Value: 0","Value: 42","Value: -3.14", etc.
boolean → Converts to "true" or "false":
`Active: ${boolean}`matches"Active: true"or"Active: false"
bigint → Represents any bigint's string form:
`ID: ${bigint}`matches"ID: 123n"style strings
null → Becomes the literal string "null":
`Result: ${null}`matches only"Result: null"
undefined → Becomes the literal string "undefined":
`Status: ${undefined}`matches only"Status: undefined"
Practical Implications
This feature lets you create type-safe patterns for formatted strings, CSS properties, event names, or any domain where you combine fixed text with variable primitive values. The types remain precise: a template with boolean will only accept "true" or "false" in that position, not arbitrary strings.
Key Takeaway: Template literal types automatically convert primitive types (number, boolean, bigint, null, undefined) into their string representations, enabling type-safe patterns that mirror JavaScript's runtime template literal behavior.