How to Create an Array in TypeScript
Use arrays in TypeScript when you need to store multiple values of the same type in one ordered structure. Arrays are perfect for lists, collections, queues, API results, and mapped UI data.
What you’ll build or solve
You’ll learn how to create arrays in TypeScript with explicit types, shorthand syntax, and inferred values. You’ll also know when tuple types are a better fit.
Learn TypeScript on Mimo
When this approach works best
This approach is the right choice when the collection can grow or shrink, and every item follows the same type.
Common real-world scenarios include:
- User lists
- Product collections
- Route definitions
- API results
- Score histories
This is a bad idea when each position in the collection has a different meaning, where tuples are clearer.
Prerequisites
You only need:
- Basic TypeScript types
- Familiarity with arrays
- Understanding of type annotations
Step-by-step instructions
Step 1: Create an array with explicit type syntax
The most common syntax is Type[].
const scores: number[] = [95, 88, 76];
This ensures every item is a number.
The same pattern works for strings.
const names: string[] = ["Alex", "Sam", "Jordan"];
This is the cleanest default array syntax.
Step 2: Use generic array syntax
TypeScript also supports Array<Type>.
const emails: Array<string> = ["a@example.com", "b@example.com"];
This is useful when working with nested generic types.
Step 3: Let TypeScript infer the type
Inference works well when the initial values are clear.
const tags = ["react", "typescript", "seo"];
TypeScript infers this as string[].
What to look for:
Type[]is the cleanest defaultArray<Type>is equivalent- Inference works from initial values
- Great for dynamic collections
- Use tuples for fixed mixed shapes
Examples you can copy
Number list
const ids: number[] = [1, 2, 3];
String tags
const tags: string[] = ["css", "html"];
Generic syntax
const users: Array<string> = ["Alex"];
Common mistakes and how to fix them
Mistake 1: Mixing incompatible values
What the reader might do:
const values: number[] = [1, "two"];
Why it breaks: all items must match the array type.
Corrected approach:
Use one consistent type or a union.
Mistake 2: Using tuples for normal lists
What the reader might do:
const names: [string, string, string]
Why it breaks: tuples lock position and length.
Corrected approach:
Use string[] for flexible lists.
Mistake 3: Over-relying on empty-array inference
What the reader might do:
const items = [];
Why it breaks: the inferred type may become too broad.
Corrected approach:
Type empty arrays explicitly.
Troubleshooting
If pushes error later, explicitly type the empty array.
If item types conflict, switch to unions.
If length should stay fixed, use tuples.
If nested arrays get complex, prefer generic syntax.
Quick recap
- Use
Type[]for arrays Array<Type>is equivalent- Inference works from initial values
- Use tuples for fixed mixed positions
- Explicitly type empty arrays
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot