Array element types
Concept and syntax
Use number[] or Array<number> to describe an array of numeric elements. The bracket form follows the element type, not the variable name. Array methods such as push must accept compatible elements. Indexing starts at zero, and even a typed array can be empty; an unchecked index may be missing at runtime. A later compiler option can make this absence explicit in the static type.
Working example
Compile standalone examples in a fresh directory with tsc --strict --target ES2022 --module commonjs --noEmitOnError main.ts, then node main.js.
const prices: number[] = [3, 5];prices.push(7);const labels: Array<string> = ["tea", "coffee"];console.log(prices[0], prices.length, labels[1]);Expected output (one line per log):
3 3 coffeeCommon mistake
Intentionally invalid; check this snippet separately. Do not run it.
Expected compiler diagnostic: TS2345. The push method accepts numeric elements, not strings. Supply 7 rather than "7".
Practice and recap
Create an empty number[] and push two valid numbers. Try pushing text, then read the array length.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Array element types — knowledge check
Check the concept, apply it to a new choice, and interpret a compiler diagnostic.
Which annotation represents an array of strings?
Can number[] be empty?
Which argument can prices.push accept without a type change?
const prices: number[] = [3, 5];prices.push("7");