Comprehensive TypeScript string tutorial covering declaration, manipulation methods, and best practices with practical examples.
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.
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.
This example shows different ways to declare TypeScript strings.
declaring_strings.ts
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.
This example demonstrates essential string manipulation methods.
string_methods.ts
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.
This example shows how to use template literals for dynamic strings.
string_interpolation.ts
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.
This example demonstrates basic string validation techniques.
string_validation.ts
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.
This example shows how to access individual characters in a string.
string_iteration.ts
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.
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
TypeScript String Documentation
This tutorial covered essential string operations in TypeScript, providing foundational knowledge for text processing in type-safe applications.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all TypeScript tutorials.