In this section, we will cover the basic types available in TypeScript. Understanding these types is fundamental to writing type-safe code. TypeScript provides several built-in types that help you define the shape and behavior of your data.

Key Concepts

  1. Primitive Types: These are the most basic types in TypeScript.

    • number
    • string
    • boolean
    • null
    • undefined
    • symbol
    • bigint
  2. Array Types: TypeScript allows you to define arrays of specific types.

  3. Tuple Types: Tuples are arrays with a fixed number of elements where each element may be of a different type.

  4. Enum Types: Enums allow you to define a set of named constants.

  5. Any Type: A type that can hold any value.

  6. Void Type: Represents the absence of any type, commonly used for functions that do not return a value.

  7. Never Type: Represents the type of values that never occur.

Primitive Types

Number

TypeScript supports all number types, including integers and floating-point numbers.

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

String

Strings are used to represent text data.

let firstName: string = "John";
let lastName: string = "Doe";

Boolean

Booleans represent logical values: true or false.

let isActive: boolean = true;
let hasPermission: boolean = false;

Null and Undefined

null and undefined are types that have only one value each: null and undefined.

let emptyValue: null = null;
let notAssigned: undefined = undefined;

Symbol

Symbols are unique and immutable primitive values.

let uniqueId: symbol = Symbol("id");

BigInt

BigInt is used for arbitrarily large integers.

let bigNumber: bigint = 1234567890123456789012345678901234567890n;

Array Types

Arrays can be defined to hold elements of a specific type.

let numbers: number[] = [1, 2, 3, 4, 5];
let names: string[] = ["Alice", "Bob", "Charlie"];

Alternatively, you can use the generic array type:

let numbers: Array<number> = [1, 2, 3, 4, 5];
let names: Array<string> = ["Alice", "Bob", "Charlie"];

Tuple Types

Tuples allow you to define an array with a fixed number of elements of specific types.

let person: [string, number] = ["Alice", 30];

Enum Types

Enums allow you to define a set of named constants.

enum Color {
  Red,
  Green,
  Blue
}

let favoriteColor: Color = Color.Green;

Any Type

The any type can hold any value. It is useful when you don't know the type of a value in advance.

let randomValue: any = 42;
randomValue = "Hello";
randomValue = true;

Void Type

The void type is used for functions that do not return a value.

function logMessage(message: string): void {
  console.log(message);
}

Never Type

The never type represents values that never occur. It is often used for functions that always throw an error or never return.

function throwError(message: string): never {
  throw new Error(message);
}

Practical Exercises

Exercise 1: Define Basic Types

Define variables with the following types: number, string, boolean, null, undefined, symbol, and bigint.

let age: number = 30;
let name: string = "Alice";
let isStudent: boolean = true;
let empty: null = null;
let notAssigned: undefined = undefined;
let uniqueId: symbol = Symbol("id");
let largeNumber: bigint = 12345678901234567890n;

Exercise 2: Define an Array and a Tuple

Define an array of numbers and a tuple with a string and a number.

let scores: number[] = [85, 90, 92];
let student: [string, number] = ["Alice", 25];

Exercise 3: Use Enums

Define an enum for days of the week and use it to set a variable.

enum Day {
  Monday,
  Tuesday,
  Wednesday,
  Thursday,
  Friday,
  Saturday,
  Sunday
}

let today: Day = Day.Wednesday;

Summary

In this section, we covered the basic types available in TypeScript, including primitive types, arrays, tuples, enums, and special types like any, void, and never. Understanding these types is crucial for writing type-safe code in TypeScript. In the next section, we will explore type annotations and how to use them effectively in your code.

© Copyright 2024. All rights reserved