Data types in TypeScript
In TypeScript, a data type defines the kind of values a variable can hold, ensuring type safety and enhancing code clarity.
- Primitive Types: Basic types like number, string, boolean, null, undefined, and symbol.
- Object Types: Complex structures including arrays, classes, interfaces, and functions.
Primitive Types
Primitive types are the most basic data types in TypeScript. They represent simple, immutable values and are directly assigned.
| Type | Keyword | Description |
|---|---|---|
| Number | number |
Represents both integer and floating-point numbers. |
| String | string |
Represents textual data. |
| Boolean | boolean |
Represents logical values: true or false. |
| Null | null |
Represents the intentional absence of any object value. |
| Undefined | undefined |
Represents an uninitialized variable. |
| Symbol | symbol |
Represents a unique, immutable value, often used as object keys. |
| BigInt | bigint |
Represents integers with arbitrary precision. |
Object Types
Object types are more complex structures that can contain multiple values and functions. They are mutable and can be manipulated after creation.
| Type | Description |
|---|---|
| Object | Represents any non-primitive type; however, its use is discouraged in favor of more specific types. |
| Array | Represents a collection of elements of a specific type. |
| Tuple | Represents an array with a fixed number of elements of specific types. |
| Enum | Represents a set of named constants, allowing for a collection of related values. |
| Function | Represents a callable entity; can define parameter and return types. |
| Class | Defines a blueprint for creating objects with specific properties and methods. |
| Interface | Describes the shape of an object, specifying property names and types. |
Advanced Types
TypeScript also offers advanced types that provide additional capabilities for complex type definitions:
| Type | Description |
|---|---|
| Union Types | Allows a variable to hold one of several types, providing flexibility in type assignments. |
| Intersection Types | Combines multiple types into one, requiring a value to satisfy all included types. |
| Literal Types | Enables exact value types, allowing variables to be assigned specific values only. |
| Mapped Types | Creates new types by transforming properties of an existing type according to a |
🔹 1. Basic (Primitive) Types
These are the most commonly used:
-
number→ for all numeric values (integers & decimals)let age: number = 25; let price: number = 99.99; -
string→ for textlet name: string = "Alice"; -
boolean→ true/false valueslet isLoggedIn: boolean = true; -
null&undefined→ represent absence of valuelet empty: null = null; let notAssigned: undefined = undefined;
🔹 2. Special Types
-
any→ disables type checking (use carefully)let data: any = "Hello"; data = 42; // allowed -
unknown→ safer alternative toanylet value: unknown = "Hi"; -
void→ used for functions that don’t return anythingfunction log(): void { console.log("Hello"); } -
never→ for values that never occur (e.g., errors, infinite loops)function throwError(): never { throw new Error("Error!"); }
🔹 3. Arrays & Tuples
-
Array → collection of same type
let numbers: number[] = [1, 2, 3]; let names: Array<string> = ["A", "B"]; -
Tuple → fixed-length array with specific types
let person: [string, number] = ["Alice", 25];
🔹 4. Objects
Define structured data:
let user: { name: string; age: number } = {
name: "John",
age: 30
};
🔹 5. Enum
Represents a set of named constants:
enum Direction {
Up,
Down,
Left,
Right
}
🔹 6. Union & Intersection Types
-
Union (
|) → value can be multiple typeslet id: number | string = 101; -
Intersection (
&) → combines multiple typestype A = { name: string }; type B = { age: number }; type Person = A & B;
🔹 7. Literal Types
Restrict values to specific constants:
let direction: "left" | "right";
direction = "left"; // valid
🔹 8. Type Aliases & Interfaces
-
Type Alias
type User = { name: string; age: number }; -
Interface
interface User { name: string; age: number; }