Course contentsShow
← TypeScript
Lesson 88 of 1,7963. Basic TypesFree lesson

The string Type

Learn how TypeScript types text data with the string type, including template literals.

The string Type

What you'll learn: How TypeScript uses the string type to represent and validate text data in your programs.

What Is the string Type?

In TypeScript, the string type represents textual data — any sequence of characters like words, sentences, or even empty text. Just like number handles numeric values and boolean handles true/false, string is TypeScript's way of saying "this variable holds text."

Think of it like labeling a container: when you mark something as string, TypeScript knows it should contain text and will warn you if you try to put something else in there.

How to Use the string Type

You can declare string variables using type annotations:

let firstName: string = "Alice";
let greeting: string = 'Hello, World!';
let emptyText: string = "";

Notice you can use either double quotes (") or single quotes (') — both create strings.

Template Literals

TypeScript also supports template literals (backticks), which let you embed expressions inside strings:

let userName: string = "Bob";
let age: number = 25;
let message: string = `Hello, ${userName}! You are ${age} years old.`;

The ${} syntax inside backticks lets you insert variables directly into your text. TypeScript ensures the overall result is still a string type.

Type Safety in Action

TypeScript catches mistakes early:

let productName: string = "Laptop";
productName = 42; // Error: Type 'number' is not assignable to type 'string'

Key Takeaway: The string type represents text data in TypeScript, supports both quotes and template literals with ${} expressions, and helps catch type mismatches at compile-time.