Number and String Types

Chapter: TypeScript Type System Fundamentals / Section: Basic Types in TypeScript

Number and String Types

A comprehensive guide to Number and String Types in TypeScript. Learn about working with numeric and string data with clear explanations. Perfect for beginners starting with TypeScript.

Introduction

Mastering data types is a fundamental skill for any TypeScript developer. Number and String types are among the most common data types you'll work with. Understanding how to declare, manipulate, and work with these types is essential for building robust TypeScript applications.

In this guide, you'll learn the core concepts of Number and String types, how to implement them effectively, best practices to follow, common pitfalls to avoid, and see practical examples to solidify your understanding.

Core Concepts

In TypeScript, the Number type represents numeric values, including integers and floating-point numbers. You can declare a variable of type Number using the number keyword:

let age: number = 25; let price: number = 9.99;

The String type represents textual data and is declared using the string keyword:

let name: string = "John"; let message: string = 'Hello, TypeScript!';

TypeScript also supports template literals, allowing you to embed expressions within strings:

let greeting: string = `Hello, ${name}!`;

Implementation Details

When working with Number and String types, you can perform various operations and use built-in methods. Here are some common examples:

  1. Number Operations:

    let x: number = 10; let y: number = 5; let sum: number = x + y; let difference: number = x - y; let product: number = x * y; let quotient: number = x / y;
  2. String Operations:

    let firstName: string = "John"; let lastName: string = "Doe"; let fullName: string = firstName + " " + lastName; let length: number = fullName.length; let uppercase: string = fullName.toUpperCase(); let substring: string = fullName.substring(0, 4);

Best Practices

  1. Use descriptive variable names to enhance code readability.
  2. Explicitly declare variable types for clarity and type safety.
  3. Use template literals for string interpolation.
  4. Be aware of type coercion when performing operations between different types.

Common Pitfalls

  1. Mixing up variable types can lead to unexpected behavior. Ensure variables are of the expected type.
  2. Be cautious when performing arithmetic operations on mixed types, as type coercion can occur.
  3. Avoid using the any type unless absolutely necessary, as it defeats the purpose of TypeScript's type checking.

Practical Examples

  1. Calculating the area of a rectangle:

    let width: number = 10; let height: number = 5; let area: number = width * height; console.log(`The area is: ${area}`);
  2. Concatenating strings:

    let firstName: string = "John"; let lastName: string = "Doe"; let fullName: string = `${firstName} ${lastName}`; console.log(`Full name: ${fullName}`);

Summary and Next Steps

In this guide, you learned about Number and String types in TypeScript. You explored core concepts, implementation details, best practices, and common pitfalls. You also saw practical examples to reinforce your understanding.

To further enhance your TypeScript skills, consider exploring more advanced topics such as type inference, union types, and type aliases. Practice using Number and String types in different scenarios to solidify your knowledge.