Ad space (banner)
Lesson 4 / 20

Array Types

This lesson covers how to type an array in TypeScript. It's for anyone searching "TypeScript array type."

An array's type is written as type-name[]. For example, an array that only holds numbers is written number[], and one that only holds strings is string[].

The example creates a numeric array, const scores: number[] = [80, 95, 70];, and computes the total with the reduce method. Since the array's element type is guaranteed, you can safely use number-specific methods.

A common early mistake is trying to mix a value of a different type into the array and hitting an error. If you genuinely want to hold both strings and numbers, you'd reach for a union type ((number | string)[]), covered in a later lesson.

In real projects, having the array's element type guaranteed cuts down on the mental overhead of reading code and wondering "what's actually in this array?"

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run TypeScript directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)