Typed Arrays
Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 13 of 73.
Typed arrays are one of TypeScript's most powerful features for organizing and managing multiple values while maintaining type safety.
In TypeScript, you can specify that an array should only contain elements of a particular type using the Type[] syntax. This ensures that every element in the array matches the expected type, preventing common errors that occur when mixing different data types.
let numbers: number[] = [1, 2, 3, 4, 5];
let names: string[] = ["Alice", "Bob", "Charlie"];
let flags: boolean[] = [true, false, true];When you declare a typed array, TypeScript enforces that only values of the specified type can be added to it. This means you can't accidentally mix strings with numbers or other incompatible types, which helps catch errors during development rather than at runtime.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
Challenge
EasyCreate a typed array named scores that can only hold numbers and initialize it with the values 85, 92, and 78.
Then create another typed array named studentNames that can only hold strings and initialize it with the values "Alice", "Bob", and "Charlie".
Finally, create a typed array named passedExams that can only hold boolean values and initialize it with true, false, and true.
Print all three arrays to the console on separate lines in the order they were created.
Cheat sheet
Typed arrays in TypeScript ensure that arrays only contain elements of a specific type using the Type[] syntax:
let numbers: number[] = [1, 2, 3, 4, 5];
let names: string[] = ["Alice", "Bob", "Charlie"];
let flags: boolean[] = [true, false, true];TypeScript enforces type safety by preventing you from mixing incompatible types in the same array, catching errors during development.
Try it yourself
// TODO: Write your code here
// Create the three typed arrays as described in the challenge
// Print all three arrays to the consoleThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Introduction To TypeScript
1Getting Started with TS
What is TypeScript?Why Use TypeScript?Your First TypeScript CodeCompilation Process & ErrorsRecap: Introduction to TS4Working with Functions
Typing Params & Return ValuesTyping Arrow FunctionsThe 'void' Return TypeOptional Parameters with '?'Default Parameter ValuesTyping Rest ParametersDefining Function TypesRecap: Building Typed Funcs2Core Types
Basic Types: str, num, booleanThe 'any' Type: Escape HatchThe 'unknown' TypeWorking with 'null' & 'undef'Type Inference in ActionExplicit Type AnnotationsRecap: Core Types Practice5Types: Aliases, Unions & Inter
Type Aliases for PrimitivesUnion Types ('|')Working with Union TypesLiteral TypesIntersection Types ('&')Combining Type AliasesRecap: Advanced Type Combos3Data Structure: Arrays & Tuple
Typed Arrays'readonly' Modifier for ArraysWhat is a Tuple?Declaring and Accessing TuplesDestructuring TuplesReadonly TuplesMulti-dimensional Typed Arrays Spread Operator with ArraysRecap: Arrays and Tuples