A TypeScript object is a structured collection of key-value pairs where keys are strings or symbols and values can be any type such as primitives, arrays, functions, or other objects. Objects allow organized storage and type-safe manipulation of data.
TypeScript Objects types
Object types in TypeScript can be defined using interfaces, type aliases, or index signatures. Interfaces and type aliases define a specific structure for objects, ensuring they follow a particular shape. Index signatures allow objects to have dynamic property names while enforcing a consistent type for their values.
Syntax:
let Name_of_object = {
object_property : value,
}In the above syntax:
- Name_of_object: This is the name of the object.
- object_property: This is the property of the object or Key.
- value: This is the value of the property.
1. Object Literal Notation
Object literal notation in TypeScript involves creating objects directly using curly braces {} with key-value pairs. It ensures type safety by defining object properties and their types explicitly.
Now let's understand this with the help of example:
JavaScript
const person = {
firstName: "Rahul",
lastName: "Kushwaha",
age: 30,
};
console.log(`Hello, ${person.firstName} ${person.lastName}!`);
OutputHello, Rahul Kushwaha!
In this example:
- person is an object with keys firstName, lastName, and age.
- person.firstName and person.lastName access the object’s properties.
- Template literals (`${}`) insert these values into the string.
2. Defining Object Types
In TypeScript, we represent object types using either interfaces or type aliases. These type templates ensure that objects adhere to a specific structure:
Using Interfaces: Interfaces define the structure of an object, specifying property names and types. This ensures objects adhere to a specific shape, enhancing type safety and code maintainability.
JavaScript
interface MyObject {
company: string;
type: string;
solve: () => string;
}
const obj: MyObject & { [key: string]: any } = {
company: 'GeeksforGeeks',
type: 'unique',
solve: function (this: MyObject) {
return `GFG is ${this.company}, and it's ${this.type}`;
}
};
console.log(obj.solve());
obj.location = "Noida";
console.log(obj.location);
Output
GFG is GeeksforGeeks, and its unique
Noida
In this example:
- MyObject interface defines company, type, and a solve method.
- obj implements this interface and can have dynamic properties due to { [key: string]: any }.
- obj.solve() uses this to return a string with company and type.
- A new property location is added dynamically and printed.
Using Type Aliases: Type aliases in TypeScript create a custom name for a specific type, including object shapes. This method defines the structure and types of object properties, ensuring consistent and type-safe objects.
JavaScript
type Product = {
productId: string;
name: string;
price: number;
};
const laptop: Product = {
productId: "LAP123",
name: "Dell Inspiron",
price: 800,
};
console.log(laptop);
Output:
{
"productId": "LAP123",
"name": "Dell Inspiron",
"price": 800
} In this example:
- Product is a type alias defining an object structure with productId, name, and price.
- laptop is an object of type Product with specific values.
- console.log(laptop) prints the object.
Index Signatures: Index signatures in TypeScript allow objects to have dynamic keys with specified value types. This is useful for objects without a fixed set of properties, enabling flexible and type-safe property access.
JavaScript
let nameAgeMap: { [index: string]: number } = {};
nameAgeMap["Jack"] = 25;
nameAgeMap["Mark"] = 30;
console.log(nameAgeMap);
Output:
{
"Jack": 25,
"Mark": 30
}
Explore
TypeScript Tutorial
8 min read
TypeScript Basics
TypeScript primitive types
TypeScript Object types
TypeScript other types
TypeScript combining types
TypeScript Assertions
TypeScript Functions
TypeScript interfaces and aliases
TypeScript classes