0Pricing
TypeScript Academy · Lesson

Typed Arrays: T[] and Array<T>

Type arrays and prevent mixing incompatible values.

Typed Arrays: T[] and Array<T> is a free TypeScript Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Welcome

TypeScript lets you type arrays so that only values of a specific type can be stored. This lesson covers the two syntax forms and their use cases.

The T[] Syntax

The most common way to type an array is to append [] to the element type. This is called the shorthand syntax.
const numbers: number[] = [1, 2, 3];
const names: string[] = ['Alice', 'Bob'];
const flags: boolean[] = [true, false, true];

The Array<T> Generic Syntax

The generic syntax Array is equivalent to T[]. Some teams prefer it for consistency with other generic types.
const scores: Array<number> = [95, 87, 73];
const tags: Array<string> = ['typescript', 'beginner'];

Type Errors in Arrays

If you try to add a value of the wrong type, TypeScript reports an error. This prevents mixed-type arrays from causing bugs.
const ids: number[] = [1, 2, 3];
// ids.push('four'); // Error: Argument of type 'string' is not assignable to 'number'

Inferred Array Types

If you initialize an array with values, TypeScript infers the element type. An empty array infers `never[]` — always annotate empty arrays.
const nums = [1, 2, 3];    // inferred: number[]
const mixed = [1, 'hi'];   // inferred: (string | number)[]
const empty: string[] = []; // must annotate

Arrays of Objects

You can type an array of objects using an interface or type alias as the element type.
interface User { id: number; name: string; }
const users: User[] = [
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' },
];

Common Array Methods Are Typed

TypeScript understands built-in array methods. map, filter, and find all return properly typed results.
const nums: number[] = [1, 2, 3, 4];
const doubled: number[] = nums.map(n => n * 2);
const evens: number[] = nums.filter(n => n % 2 === 0);

Nested Arrays

Type a 2D array by nesting the array type syntax.
const matrix: number[][] = [
  [1, 2, 3],
  [4, 5, 6],
];

Union Arrays

To allow multiple types in an array, use a union as the element type.
const mixed: (string | number)[] = ['Alice', 1, 'Bob', 2];

Array Destructuring with Types

When you destructure an array, TypeScript infers the types of the destructured variables.
const coords: number[] = [10, 20];
const [x, y] = coords; // x: number, y: number

Choosing T[] vs Array<T>

Both forms are identical to TypeScript. Use T[] for simple types. Use Array when the element type is long or when using other generics for visual clarity.

Quick Check

What does TypeScript infer for an empty array declared as `const list = []`?

Recap

Use T[] or Array to type arrays in TypeScript. Always annotate empty arrays. TypeScript enforces the element type on push, map, filter, and other array operations.

Frequently asked questions

Is the “Typed Arrays: T[] and Array<T>” lesson free?

Yes — the full text of “Typed Arrays: T[] and Array<T>” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Typed Arrays: T[] and Array<T>”?

Type arrays and prevent mixing incompatible values. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Typed Arrays: T[] and Array<T>” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Typed Arrays: T[] and Array
  2. Readonly Arrays and Immutable Data
  3. Tuples: Fixed-Length Typed Arrays
  4. Enums: Numeric and String Constants
← Back to TypeScript Academy