How does string[] differ from Array<string>?
1. The main difference
| Notation | What it means | Equivalent |
|---|---|---|
string[] | "an array of strings" | Array<string> |
Array<string> | "an array of elements of type string (via a generic)" | string[] |
So both forms mean exactly the same thing: an array of strings. The only difference is in syntax and readability.
Example 1 - equivalent declarations
const a: string[] = ["a", "b", "c"];
const b: Array<string> = ["a", "b", "c"];Both arrays are identical in type and behavior. The TypeScript compiler sees no difference between them.
2. When string[] is better, and when Array<string> is
string[] - simpler and shorter
Used in 90% of cases, when the type is a primitive or a simple object.
const nums: number[] = [1, 2, 3];
const names: string[] = ["Tim", "Max"];Array<Type> - more convenient for complex types
When the inner type is a generic, a union, an intersection, or a function:
type User = { id: number; name: string };
const users: Array<User> = [
{ id: 1, name: "Tim" },
{ id: 2, name: "Max" },
];or
const mixed: Array<string | number> = ["a", 1, "b", 2];Here
Array<string | number>reads a little easier than(string | number)[].
Example with a complex function type
type Callback = () => void;
const callbacks1: Callback[] = [];
const callbacks2: Array<Callback> = [];
callbacks1.push(() => console.log("ok"));
callbacks2.push(() => console.log("ok"));Both variants work the same way, but
Array<Callback>can be a bit clearer if the type is long.
3. Technically, T[] is just "syntactic sugar"
Internally, TypeScript treats both forms the same way. They are two ways of writing the same generic definition:
T[] === Array<T>
T[]is the short notation,Array<T>is the full generic-type notation.
4. Special cases: ReadonlyArray
If you use immutable arrays, Array<T> gives you more options.
const arr: ReadonlyArray<number> = [1, 2, 3];
arr.push(4); // ErrorFor
readonly number[]andReadonlyArray<number>the behavior is the same, butReadonlyArraycan be conveniently combined with generics:
function freeze<T>(arr: ReadonlyArray<T>): void {}5. Example with multidimensional arrays
Both forms are equivalent and read differently:
const matrix1: number[][] = [[1, 2], [3, 4]];
const matrix2: Array<Array<number>> = [[1, 2], [3, 4]];Here
number[][]is visually simpler, whileArray<Array<number>>is more formal.
Summary
| Comparison | string[] | Array<string> |
|---|---|---|
| Brevity | shorter | longer |
| Convenient for complex types | medium | reads better |
| Support for generic functions | yes | yes |
| Semantics | identical | identical |
| Frequency of use | very high | less common (more formal style) |
Rule of thumb:
- For simple cases -
T[](string[],number[],User[])- For complex generic types -
Array<T>(Array<User | Admin>,Array<() => void>)
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.