How do you type an array of a specific element type, and what are the two equivalent syntaxes?
Short answer
Use either `Type[]` (more common, especially for simple types) or the generic `Array ` syntax (sometimes preferred for more complex or nested generic types, for readability) — both are functionally identical.
Advertisement
AModel answer
Use either `Type[]` (more common, especially for simple types) or the generic `Array<Type>` syntax (sometimes preferred for more complex or nested generic types, for readability) — both are functionally identical.
let scores: number[] = [90, 85, 78];
let names: Array<string> = ['Sam', 'Alex'];
// Array<Array<number>> can be more readable than number[][]Real-world example
Typing a function parameter that accepts a list of user objects or numeric scores.
Likely follow-ups
- Which syntax is generally preferred by style guides, and does it matter functionally?
Related topics