We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Type Parameters

TypeScript offers an alternative way to declare arrays using type parameter syntax: Array<T>, which, for now, just know that it's basically the same as the "normal" T[] syntax. You'll see both versions in the wild.

These function declarations are the same:

// Using bracket notation
function assignLightsaberColors(name: string, colors: string[]): void {
  // ...
}
// Using generic type parameter syntax
function assignLightsaberColors(name: string, colors: Array<string>): void {
  // ...
}

You can also use either syntax when declaring variables:

const colors: string[] = [
  "blue",
  "green",
  "purple",
  "red",
  "orange",
  "white",
  "darksaber",
];
const midichlorianCounts: Array<number> = [
  1000, 5000, 12000, 20000, 27000, 40000,
];

Later, when we talk about generics, it will make a bit more sense why you might use Array<T> over T[] - and the answer is mostly because it will feel consistent with other generic types.

In the common case, I prefer number[] over Array<number>. It looks like an array (square brackets) and it's a bit faster to type.