Quick Answer

Arrays with specific element types

Understanding the Issue

TypeScript allows specifying array element types using either Type[] or Array<Type> syntax. This ensures all array elements conform to the specified type.

The Problem

This code demonstrates the issue:

Typescript Error
// Problem: Untyped array
const numbers = [1, 2, 3]; // Implicit any[] without strict

The Solution

Here's the corrected code:

Typescript Fixed
// Solution 1: Type annotation
const numbers: number[] = [1, 2, 3];

// Solution 2: Generic syntax
const names: Array<string> = ["Alice", "Bob"];

// Solution 3: Readonly array
const readonlyNums: ReadonlyArray<number> = [1, 2, 3];

Key Takeaways

Always explicitly type arrays to maintain type safety.