Array<T> Array Types
Last updated: 2026-10-22
Array<T> Array Types
Array types describe "a list of same-typed elements", written either T[] or Array<T> — fully equivalent. Use them for lists, collections and batches.
| Group | Basic Types |
|---|---|
| Kind | Type |
| Strict mode | Not required |
ℹ️ Info The editor transpiles TypeScript to JavaScript and runs it (no type checking); see each example's Output for type-check results.
📝 Syntax
let ids: number[] = [1, 2, 3];
let names: Array<string> = ["Ada", "Li"];
let mixed: (string | number)[] = [1, "a"];
⚙️ Parameters
| T[] | Concise form, the everyday choice |
|---|---|
| Array<T> | Generic form; more readable when the element type is complex |
Returns:None — array types are type-level only; at runtime it is a plain JS array.
▶ Example
Edit the code below and click Try it to see the transpiled result:
Output:
pen, book, bag
total: 50.5
(Runtime output after transpilation; pushing a string into prices is a compile error)
💡 Notes
ℹ️ Info T[] and Array<T> are identical — pick one style per team and stick to it.
⚠️ Note Array types constrain neither length nor order; for "fixed length, per-position meaning" use a tuple.
💡 Tip readonly T[] blocks accidental push/splice and signals that a function will not mutate the array.
🏷️ Related Basic Types
💡 Related cases
More cases coming soon.
📚 Related tutorials
❓ FAQ
Q How do I type an array whose elements may be missing?
A Use (T | undefined)[] and narrow or use ?. after access; do not lie with T[].
Q Array vs tuple?
A Arrays are variable-length with one element type; tuples are fixed-length with per-position types. Coordinates [number, number] → tuple; cart items → array.
Q What type does const arr = [] get?
A Under strict mode it becomes an "evolving array": starting as any[] and narrowing as you push. Prefer explicit annotations over this implicit behavior.