- 01Getting Started
- 02Your First Output (console.log)
- 03Variables and Type Annotations (number/string/boolean)
- 04Array Types
- 05Function Types (Parameters and Return Values)
- 06Optional Parameters and Default Parameters
- 07Interfaces (interface)
- 08Optional Properties (?)
- 09Type Aliases (type)
- 10Union Types
- 11Literal Types
- 12enum (Enumerated Types)
- 13Classes and Types
- 14Inheritance (extends) and Implementing an Interface (implements)
- 15Generics (<T>)
- 16Tuple Types
- 17The any and unknown Types
- 18Type Assertions (as)
- 19The readonly Modifier
- 20[Applied] Build a Mini Inventory Management System
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?"
๐งช 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).
