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.

GroupBasic Types
KindType
Strict modeNot 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.
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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏