TypeScript Strings
last modified February 24, 2025
TypeScript strings store textual data as character sequences. They extend JavaScript strings with type annotations for better code safety. This guide explores declaration, manipulation methods, and practical use cases.
What Are TypeScript Strings?
Strings in TypeScript represent immutable sequences of Unicode characters. They can be declared with single quotes, double quotes, or template literals. Type annotations ensure only string values are assigned.
Creating Strings
This example shows different ways to declare TypeScript strings.
let greeting: string = "Welcome"; let filename: string = 'app.js'; let path: string = `C:/projects/${filename}`; console.log(path); // Output: C:/projects/app.js
Template literals (backticks) allow embedded expressions. All declarations create immutable string values with type safety.
String Methods
This example demonstrates essential string manipulation methods.
let message: string = "TypeScript Essentials"; console.log(message.charAt(4)); // Output: S console.log(message.slice(4, 10)); // Output: Script console.log(message.includes("Ess")); // Output: true console.log(message.replace("Essentials", "Mastery")); // Output: TypeScript Mastery
Common methods include charAt
for character access,
slice
for substring extraction, and replace
for
pattern substitution.
String Interpolation
This example shows how to use template literals for dynamic strings.
let user: string = "Alice"; let tasks: number = 5; console.log(`${user} has ${tasks} pending tasks`); // Output: Alice has 5 pending tasks
Template literals simplify string concatenation and support multi-line strings without escape characters.
String Validation
This example demonstrates basic string validation techniques.
function isValidEmail(email: string): boolean { return email.includes("@") && email.includes("."); } console.log(isValidEmail("user@example.com")); // Output: true console.log(isValidEmail("invalid.email")); // Output: false
Basic validation checks can be implemented using includes
and
other string methods. For production, use regular expressions.
Iterating Over Strings
This example shows how to access individual characters in a string.
let charset: string = "ABCD"; for (let char of charset) { console.log(char); } // Output: // A // B // C // D
The for...of
loop iterates through each character. Strings are
iterable in TypeScript/JavaScript.
Best Practices
- Use Template Literals: Improve readability with embedded expressions
- Prefer const: Declare fixed strings with
const
- Sanitize Inputs: Validate and trim user input strings
- Use Methods Wisely: Choose between
slice
,substring
based on needs - Handle Encoding: Consider Unicode characters in modern apps
Source
TypeScript String Documentation
This tutorial covered essential string operations in TypeScript, providing foundational knowledge for text processing in type-safe applications.
Author
List all TypeScript tutorials.