Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Array Types | Core TypeScript Types
TypeScript Foundations

bookArray Types

Arrays let you store and manage ordered collections of data. In TypeScript, arrays are type-safe, which means you define what type of elements an array can hold. This helps you catch errors early and makes your code more predictable. You declare an array type in two main ways: by adding square brackets to a type, like number[], or by using the generic Array<number> syntax. Both forms mean the same thing—a list where every element is a number. You can do the same with other types, such as string[] or Array<string>, to create arrays of strings.

index.ts

index.ts

copy

Sometimes, you want to make sure an array cannot be changed after it's created. TypeScript provides the readonly modifier for this purpose. A readonly array means you cannot add, remove, or modify its elements. You declare a readonly array using readonly number[] or ReadonlyArray<number>. This is useful when you want to prevent accidental changes to data that should stay constant. As a best practice, always specify the element type when declaring an array, and use readonly if you want to guarantee immutability. This makes your code safer and easier to maintain.

question mark

Which of the following is the correct syntax to declare an array of numbers that can be modified in TypeScript?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 6

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Suggested prompts:

Can you show examples of how to declare and use arrays in TypeScript?

What happens if I try to modify a readonly array?

When should I use `readonly` arrays versus regular arrays?

Awesome!

Completion rate improved to 5.56

bookArray Types

Stryg for at vise menuen

Arrays let you store and manage ordered collections of data. In TypeScript, arrays are type-safe, which means you define what type of elements an array can hold. This helps you catch errors early and makes your code more predictable. You declare an array type in two main ways: by adding square brackets to a type, like number[], or by using the generic Array<number> syntax. Both forms mean the same thing—a list where every element is a number. You can do the same with other types, such as string[] or Array<string>, to create arrays of strings.

index.ts

index.ts

copy

Sometimes, you want to make sure an array cannot be changed after it's created. TypeScript provides the readonly modifier for this purpose. A readonly array means you cannot add, remove, or modify its elements. You declare a readonly array using readonly number[] or ReadonlyArray<number>. This is useful when you want to prevent accidental changes to data that should stay constant. As a best practice, always specify the element type when declaring an array, and use readonly if you want to guarantee immutability. This makes your code safer and easier to maintain.

question mark

Which of the following is the correct syntax to declare an array of numbers that can be modified in TypeScript?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 6
some-alt