DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk2 min

How to Declare and Initialize an Empty Array in TypeScript

Initialize a typed empty array with `const items: string[] = [];`. TypeScript also accepts `Array` and `readonly string[]` when appropriate.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose an array type spelling

TypeScript supports two equivalent ways to express an array of a given element type:

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 Programming Language - Software Engineer & Coder T-Shirt
  • 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Why 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.Support on Ko-Fi

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.