TypeScript Styleguide
Array Types: T[] vs Array<T>
Two syntaxes for defining array types in TypeScript. Explore the differences between the simple bracket notation and the generic Array type.
96%
Use Array Bracket Notation
4%
Use Generic Array<T> Syntax
Array Bracket Notation
The modern, concise way to define array types using T[] syntax.
Code Example
example.ts (T[] syntax)
const numbers: number[] = [1, 2, 3];
const users: User[] = [];
const nested: string[][] = [];
const readonly: readonly string[] = [];
const union: (string | number)[] = [];Key Benefits
- Concise and readable syntax
- Widely used in modern TypeScript
- Works with readonly modifier
- Supports union types directly
- Commonly preferred in popular projects
- Less verbose than generic syntax
Statistics
Generic Array<T> Syntax
The formal, generic way to define array types using Array<T> syntax.
Code Example
example.ts (Array<T> syntax)
const numbers: Array<number> = [1, 2, 3];
const users: Array<User> = [];
const nested: Array<Array<string>> = [];
const readonly: ReadonlyArray<string> = [];
const union: Array<string | number> = [];Key Benefits
- Explicit generic syntax
- Clear type parameter declaration
- Works with ReadonlyArray
- Consistent with other generics
- Familiar to developers from other languages
- More verbose but potentially clearer
Statistics
Additional Insights
More data points to help you make an informed decision for your team.
995
repositories analyzed