October 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 NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk1 min

How to Create and Use an Empty String Array in TypeScript

Use `const names: string[] = [];` to create an empty string array in TypeScript, then add strings with methods such as `push`.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Declare the element type when you initialize the array: const names: string[] = [];. This creates an empty array intended to hold strings. You can write the same type as Array<string>.

Create an empty string array

Use a type annotation to make the array’s intended element type explicit:

const names: string[] = [];

TypeScript also supports the generic form:

const otherNames: Array<string> = [];

Both string[] and Array<string> are documented ways to write an array type in TypeScript. TypeScript Handbook: Arrays

Add strings and use the array

Once declared as string[], the array can be populated with strings and used like other mutable arrays:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const tags: string[] = [];

tags.push("typescript");
tags.push("arrays");

for (const tag of tags) {
  console.log(tag);
}

console.log(tags.length); // 2

Methods such as push add elements; length reports the current number of elements.

Why annotate an array initialized with []?

An empty literal contains no example elements from which TypeScript can determine the intended element type. Inference also depends on context and compiler settings, so do not assume every bare [] receives the same type in every situation. An explicit annotation states the intent where the array is declared.

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

The TypeScript documentation specifically notes that an empty array initializer in a checked JavaScript (.js) file is treated as any[]. TypeScript’s implicit-any documentation describes errors for particular contexts; these examples do not establish one universal inference outcome for all TypeScript versions and configurations. See TypeScript: Type Inference and TypeScript 2.1 Release Notes.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose a notation and the right nullability

string[] is concise, while Array<string> makes the generic container form visible. They express the same element type; choose the form that fits the conventions in your codebase.

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

If an element may be absent or null, express that requirement in the type rather than leaving the empty array untyped. For example, with strict null checking enabled, string | undefined is distinct from string. TypeScript’s Handbook recommends enabling strictness checks for new codebases where possible: TypeScript Handbook: Basic Types.

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
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair 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.