Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Object and Array Types | Working with Complex and Composed Types
TypeScript Types Fundamentals

bookObject 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 };
copy

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.

question mark

Which of the following code snippets correctly use object or array type annotations in TypeScript?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 1

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Suggested prompts:

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

bookObject and Array Types

Veeg om het menu te tonen

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 };
copy

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.

question mark

Which of the following code snippets correctly use object or array type annotations in TypeScript?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 1
some-alt