Declare the array’s element type and initialize it with []: const items: string[] = [];. This creates an empty, mutable array of strings and makes the intended type explicit.
Declare and initialize an empty array
Put the element type after the variable name, then assign an empty array literal:
As an Amazon Associate I earn from qualifying purchases.
const items: string[] = [];
const ids: number[] = [];
The annotation tells TypeScript what values the array is intended to hold even though the empty literal provides no example elements. Choose the type based on the values your code will add.
Choose an array type spelling
TypeScript supports two equivalent ways to express an array of a given element type:
#1 Best Overall
const names: string[] = [];
const names: Array<string> = [];
T[] is concise and common. Array<T> can fit more naturally alongside other generic types. Choose the form that best matches nearby code. Both represent an array whose elements have the specified type. TypeScript Handbook: Arrays.
Use readonly when the reference should not mutate the array
If code using this reference should not push, splice, or otherwise change the array, annotate it as readonly:
Rank #2
- TypeScript implements a superset of syntax for strictly typed development, facilitating deep static analysis and enhanced development environment integration. The compiler translates source into standard script formats, ensuring parity across any runtime.
- TypeScript is ideal for front-end developers, full-stack engineers, and software architects who build large-scale web applications. It serves those looking to improve code excellence, reduce bugs through static checking, and maintain complex projects more.
- Lightweight, Classic fit, Double-needle sleeve and bottom hem
const items: readonly string[] = [];
const only prevents assigning a different array to the variable; it does not make the array contents immutable. The readonly annotation prevents mutation through that typed reference. TypeScript Handbook: The ReadonlyArray Type.
PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteWhy an explicit annotation is useful
TypeScript infers types from initial values and can also use context when an expression is assigned or passed somewhere. An empty array has no elements to indicate the intended element type, so unannotated inference can depend on the surrounding code and compiler options. Adding an explicit type, such as string[], states the intent directly rather than relying on inference details. TypeScript Handbook: Type Inference.
Empty arrays in JavaScript files checked by TypeScript
Do not assume an unannotated [] behaves identically in .js and .ts files. The TypeScript documentation specifies that in JavaScript files checked by TypeScript, an empty-array initializer has type any[], even with strictNullChecks enabled. Use JSDoc to specify the element type:
/** @type {string[]} */
const items = [];
TypeScript Handbook: Type Checking JavaScript Files.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Compiler options and inference
Compiler settings affect how TypeScript reports or handles implicit types. strict enables a family of stricter checks, while strictNullChecks changes how null and undefined are treated. noImplicitAny reports cases where an implicit any would otherwise be inferred. These settings do not change the practical recommendation: annotate an empty array when its intended element type should be clear and dependable. Consult the strict, strictNullChecks, and noImplicitAny option references for project-specific behavior.
Free tools Windows power users keep installed
One-click scans. No signup required.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




