TypeScript Basic and Object Types

TypeScript data types are categorized into primitive types and object types. Primitive types include:

  • number: Represents the numeric type, encompassing both integers and floating-point numbers.
  • string: Represents the string type.
  • boolean: Represents the boolean type, which has only two possible values: true and false.
  • null and undefined: Represent null (empty) and undefined (unassigned) values respectively.
  • void: Represents a function that returns no value.

In addition to primitive types, TypeScript supports object types, including:

  • array: Represents the array type.
  • tuple: Represents the tuple type, allowing specification of both the element types and their count within an array.
  • enum: Represents the enumeration type, enabling declaration of a set of named constants.
  • any: Represents any type, capable of being assigned arbitrary values.
  • object: Represents the object type, which can contain arbitrary properties and methods.
  • interface: Represents the interface type, allowing specification of an object's property and method types.
  • class: Represents the class type, facilitating object creation and inheritance.

Below is an example demonstrating variable declarations using various data types:

let myNumber: number = 42; // Declare a numeric variable with value 42
let myString: string = "Hello, world!"; // Declare a string variable
let myBoolean: boolean = true; // Declare a boolean variable set to true
let myNull: null = null; // Declare a null variable (TypeScript allows this assignment)
let myUndefined: undefined = undefined; // Declare an undefined variable

// Note on void type usage in assignments above: 
// In strict mode, assigning 'undefined' or other values directly might require specific handling depending on the context.
// However, for demonstration of declaration syntax:
let myVoid: void = undefined; 

let myArray: number[] = [1, 2, 3]; // Declare an array containing numbers

let myTuple: [string, number] = ["hello", 42]; // Declare a tuple with specific types and order

enum Color {Red, Green, Blue}; // Define an enumeration type named 'Color'
let myColor: Color = Color.Red; // Assign a value from the enum to a variable of that type

let myAny: any = "hello"; // Variable can hold any data type due to 'any' annotation

interface Person {name: string, age: number}; // Define an interface with properties name and age
class Student implements Person { 
    name: string; 
    age: number; 
    constructor(name: string, age: number) {
        this.name = name; 
        this.age = age;
    }
}

let myStudent: Person = new Student("John", 30); // Instantiate a class and assign to an interface variable

In this example, we have declared variables of different types, including the numeric type number, string type string, boolean type boolean, null type null and undefined type undefined, void return type void, array type array, tuple type tuple, enumeration type enum, any type any, object type object, interface type interface, and class type class.