Typescript
Menu
Object
- Objects in TypeScript are a basic data structure that allows you to store collections of key-value pairs.
- They can contain various types of data, including primitives, arrays, and functions.
- Below are the key aspects of objects in TypeScript, explained in bullet points with coding examples.
Definition of Objects
- Key-Value Pairs: An object is an instance that contains a set of key-value pairs, where the keys are strings (or symbols) and the values can be of any data type.
let person = {
firstName: "John",
lastName: "Doe",
age: 30
};
- Key-Value Pairs: An object is an instance that contains a set of key-value pairs, where the keys are strings (or symbols) and the values can be of any data type.
Creating Objects:
- Object Literals: The most common way to create an object is using object literals.
let car = {
make: "Toyota",
model: "Camry",
year: 2020
}; - Using the new Object() Syntax: You can also create an object using the Object constructor.
let bike = new Object();
bike.make = "Yamaha";
bike.model = "MT-07";
- Object Literals: The most common way to create an object is using object literals.
Accessing Object Properties:
- Dot Notation: You can access properties using dot notation.
console.log(person.firstName); // Outputs: John
- Bracket Notation: You can also access properties using bracket notation, which is useful for dynamic property names.
console.log(person["lastName"]); // Outputs: Doe
- Dot Notation: You can access properties using dot notation.
Modifying Object Properties:
- You can modify existing properties or add new ones.
person.age = 31; // Modify existing property
person.gender = "Male"; // Add new property
- You can modify existing properties or add new ones.
Deleting Object Properties:
- You can remove properties from an object using the `delete` operator.
delete person.age; // Removes the age property
- You can remove properties from an object using the `delete` operator.
Methods in Objects:
- Objects can contain functions as values, which are called methods.
let calculator = {
add: function(x: number, y: number): number {
return x + y;
}
};
console.log(calculator.add(5, 3)); // Outputs: 8
- Objects can contain functions as values, which are called methods.
Object Types and Interfaces:
- Defining Object Types: You can define the structure of an object using interfaces or type aliases.
- Using Interfaces:
interface Person {
firstName: string;
lastName: string;
age?: number; // Optional property
}
let employee: Person = {
firstName: "Alice",
lastName: "Smith"
}; - Using Type Aliases:
type Car = {
make: string;
model: string;
year: number;
};
let myCar: Car = {
make: "Honda",
model: "Civic",
year: 2022
};
- Using Interfaces:
- Defining Object Types: You can define the structure of an object using interfaces or type aliases.
Object Types vs Object Type (Uppercase)
object
Type: Represents all non-primitive values (i.e., anything that is not a primitive type)let obj1: object;
obj1 = { name: "John" }; // Valid
// obj1 = "Jane"; // Error: Type 'string' is not assignable to type 'object'.Object
Type (Uppercase): Refers to the instance of the global Object class and includes methods like `toString()`.let obj2: Object;
obj2 = { name: "Doe" };
console.log(obj2.toString()); // Outputs something like [object Object]
Empty Object Type (
{}
)- The empty type `{}` describes an object that has no properties of its own but can still access methods from the `Object` prototype.
let emptyObj: {};
emptyObj = {}; // Valid
// emptyObj.name = "John"; // Error: Property 'name' does not exist on type '{}'.
- The empty type `{}` describes an object that has no properties of its own but can still access methods from the `Object` prototype.
Conclusion
- Objects in TypeScript provide a structured way to organize and manipulate data through key-value pairs.
- By understanding how to create, access, modify, and define object types using interfaces or type aliases, you can take advantage of the full power of objects in your TypeScript applications.
- This increases code clarity, maintainability, and type safety, making it easier to work with complex data structures.