Object and Array Types
When you need to represent structured data in TypeScript, you use object and array types. Object types describe the shape of an object, specifying the names and types of its properties.
12345678910111213141516171819202122// Object type annotation type User = { id: number; name: string; isActive: boolean; }; // Array of objects const users: User[] = [ { id: 1, name: "Alice", isActive: true }, { id: 2, name: "Bob", isActive: false } ]; // Array type annotation for numbers const scores: number[] = [95, 88, 76]; // Inline object type annotation const product: { id: number; title: string; price: number } = { id: 101, title: "Book", price: 12.99 };
In the code above, the User type defines an object with three properties: id as a number, name as a string, and isActive as a boolean. You can then declare variables like users, which is an array of User objects, using the User[] syntax to indicate an array where every element matches the User type.
Array types are written by placing [] after a type, such as number[] for an array of numbers. The scores variable is typed as an array of numbers, ensuring that only numeric values can be added.
You can also define object types inline, without a named type. The product variable uses an inline object type annotation, listing the required properties and their types directly in the variable declaration. This approach is useful for single-use or simple objects when you do not need to reuse the type elsewhere.
Object and array types help you model real-world data structures accurately, allowing TypeScript to catch mistakes early and provide better code completion and documentation. By using these types, you ensure that your code is both robust and easy to understand.
Danke für Ihr Feedback!
Fragen Sie AI
Fragen Sie AI
Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen
Can you explain the difference between using a named type and an inline object type in TypeScript?
How do I add a new property to the User type?
Can you show an example of a nested object type in TypeScript?
Awesome!
Completion rate improved to 8.33
Object and Array Types
Swipe um das Menü anzuzeigen
When you need to represent structured data in TypeScript, you use object and array types. Object types describe the shape of an object, specifying the names and types of its properties.
12345678910111213141516171819202122// Object type annotation type User = { id: number; name: string; isActive: boolean; }; // Array of objects const users: User[] = [ { id: 1, name: "Alice", isActive: true }, { id: 2, name: "Bob", isActive: false } ]; // Array type annotation for numbers const scores: number[] = [95, 88, 76]; // Inline object type annotation const product: { id: number; title: string; price: number } = { id: 101, title: "Book", price: 12.99 };
In the code above, the User type defines an object with three properties: id as a number, name as a string, and isActive as a boolean. You can then declare variables like users, which is an array of User objects, using the User[] syntax to indicate an array where every element matches the User type.
Array types are written by placing [] after a type, such as number[] for an array of numbers. The scores variable is typed as an array of numbers, ensuring that only numeric values can be added.
You can also define object types inline, without a named type. The product variable uses an inline object type annotation, listing the required properties and their types directly in the variable declaration. This approach is useful for single-use or simple objects when you do not need to reuse the type elsewhere.
Object and array types help you model real-world data structures accurately, allowing TypeScript to catch mistakes early and provide better code completion and documentation. By using these types, you ensure that your code is both robust and easy to understand.
Danke für Ihr Feedback!