The existing answers missed an option, so here's a complete list:

// 1. Explicitly declare the type
var arr: Criminal[] = [];

// 2. Via type assertion
var arr = <Criminal[]>[];
var arr = [] as Criminal[];

// 3. Using the Array constructor
var arr = new Array<Criminal>();
  1. Explicitly specifying the type is the general solution for whenever type inference fails for a variable declaration.

  2. The advantage of using a type assertion (sometimes called a cast, but it's not really a cast in TypeScript) works for any expression, so it can be used even when no variable is declared. There are two syntaxes for type assertions, but only the latter will work in combination with JSX if you care about that.

  3. Using the Array constructor is something that will only help you in this specific use case, but which I personally find the most readable. However, there is a slight performance impact at runtime*. Also, if someone were crazy enough to redefine the Array constructor, the meaning could change.

It's a matter of personal preference, but I find the third option the most readable. In the vast majority of cases the mentioned downsides would be negligible and readability is the most important factor.

*: Fun fact; at the time of writing the performance difference was 60% in Chrome, while in Firefox there was no measurable performance difference.

Answer from Thorarin on Stack Overflow
Top answer
1 of 7
455

The existing answers missed an option, so here's a complete list:

// 1. Explicitly declare the type
var arr: Criminal[] = [];

// 2. Via type assertion
var arr = <Criminal[]>[];
var arr = [] as Criminal[];

// 3. Using the Array constructor
var arr = new Array<Criminal>();
  1. Explicitly specifying the type is the general solution for whenever type inference fails for a variable declaration.

  2. The advantage of using a type assertion (sometimes called a cast, but it's not really a cast in TypeScript) works for any expression, so it can be used even when no variable is declared. There are two syntaxes for type assertions, but only the latter will work in combination with JSX if you care about that.

  3. Using the Array constructor is something that will only help you in this specific use case, but which I personally find the most readable. However, there is a slight performance impact at runtime*. Also, if someone were crazy enough to redefine the Array constructor, the meaning could change.

It's a matter of personal preference, but I find the third option the most readable. In the vast majority of cases the mentioned downsides would be negligible and readability is the most important factor.

*: Fun fact; at the time of writing the performance difference was 60% in Chrome, while in Firefox there was no measurable performance difference.

2 of 7
269

The issue of correctly pre-allocating a typed array in TypeScript was somewhat obscured for due to the array literal syntax, so it wasn't as intuitive as I first thought.

The correct way would be

var arr : Criminal[] = [];

This will give you a correctly typed, empty array stored in the variable 'arr'

🌐
GeeksforGeeks
geeksforgeeks.org › typescript › how-to-declare-an-empty-array-in-typescript
How to Declare an Empty Array in TypeScript? - GeeksforGeeks
July 23, 2025 - In TypeScript, you can declare an empty array that will only accept elements of a particular type. The most common approach is using the square bracket syntax to specify the type of the array. This ensures that only numbers can be added to the ...
🌐
Tim Mousk
timmousk.com › blog › typescript-empty-array
How To Declare An Empty Array In TypeScript? – Tim Mouskhelichvili
March 6, 2023 - If you want to limit the types an empty array can accept, you can declare it explicitly. ... typescript// This array only accepts strings. const strArr: string[] = []; strArr.push('Tim'); // This array only accepts numbers.
🌐
Reddit
reddit.com › r/typescript › how do you handle empty arrays in typescript?
r/typescript on Reddit: How do you handle empty arrays in typescript?
May 30, 2022 -

SOLUTION:

simplify the load books function and check for possible null

const loadBooks = (): Ibook[] => {
  return JSON.parse(localStorage.getItem('books')!) ?? [];
}

update handleSave function to not check for empty arrays and make it less verbose

  const handleSave = (): void => {
    const savedArr: Ibook[] = [
      ...loadBooks(),
      { id, title, authors, publishingCompany, saved: true }
    ];
    localStorage.setItem('books', JSON.stringify(savedArr));
    setBookSaved(true);
  };

Original Question Below:

I have a function below that may return an empty array

const loadBooks = ():Ibook[] | [] => {
  // @ts-ignore
  let savedArr = JSON.parse(localStorage.getItem('books'));
  if (!savedArr || !Array.isArray(savedArr)) return [];
  else return savedArr;
}

I have another function that pushes a book object into the returned array

  const handleSave = () => {
    let savedArr: Ibook[] | [] = loadBooks();
    const bookObj: Ibook = { id, title, authors, publishingCompany };
    savedArr.push(bookObj);
    localStorage.setItem('books', JSON.stringify(savedArr));
  };

However, my code will not run because TypeScript says "Argument of type 'Ibook' is not assignable to parameter of type 'never'."

🌐
Delft Stack
delftstack.com › home › howto › typescript › typescript empty array
How to Create an Empty Array in TypeScript | Delft Stack
February 2, 2024 - This tutorial will show us how to create an empty array in TypeScript that can later be populated with data.
Find elsewhere
🌐
GitBook
basarat.gitbook.io › typescript › main-1 › create-arrays
Create Arrays | TypeScript Deep Dive
September 24, 2022 - Create Arrays · Typesafe Event Emitter · StyleGuide · TypeScript Compiler InternalsPowered by GitBook · On this page · For the complete documentation index, see llms.txt. This page is also available as Markdown. CopyOn this page · Creating an empty array is super easy: Copy ·
🌐
Webdevtutor
webdevtutor.net › blog › typescript-create-an-empty-array
How to Create an Empty Array in TypeScript
In this method, we simply assign an empty array literal [] to a variable. This creates an empty array of type any[], which means it can hold values of any type.
🌐
Webdevtutor
webdevtutor.net › blog › typescript-how-to-create-empty-array
TypeScript: How to Create an Empty Array
Creating an empty array in TypeScript is a fundamental operation that you will frequently encounter in your development projects. By using the Array constructor, array literal syntax, or the Array.from method, you can easily initialize empty arrays based on your requirements.
🌐
TestMu AI Community
community.testmuai.com › ask a question
How do I initialize an empty typed array in TypeScript? - Ask a Question - TestMu AI (formerly LambdaTest) Community
September 27, 2024 - I’m creating a simple logic game called “Three of a Crime” in TypeScript. When trying to pre-allocate a typed array in TypeScript, I attempted the following: var arr = Criminal[]; This resulted in the error “Check format of expression term.” I also tried: var arr: Criminal = []; This produced the error "cannot convert any[] to 'Criminal'."
🌐
DEV Community
dev.to › martinpersson › type-safety-with-non-empty-arrays-in-typescript-k78
Type Safety with Non-Empty Arrays in TypeScript - DEV Community
August 4, 2023 - const numbersArray: number[] = [] const getFirstValue = (array: number[]) => array[0]; console.log(getFirstValue(numbersArray)); // undefiend, no type error · TypeScript gives us the ability to create custom generic types and type helpers, and this can be used to create a specific constraint: a "Non-empty array" type.
🌐
Medium
medium.com › javascript-dots › using-typescript-arrays-d540c47f73b1
Using TypeScript — Arrays. Empty arrays, type inference and more. | by John Au-Yeung | JavaScript_Dots | Medium
June 18, 2020 - TypeScript is a natural extension of JavaScript that’s used in many projects in place of JavaScript. However, not everyone knows how it actually works. In this article, we’ll look at how to define and use arrays in our TypeScript code.
Author: bobbyhadz
🌐
Quora
quora.com › How-do-you-declare-an-empty-array-in-JavaScript
How to declare an empty array in JavaScript - Quora
Answer (1 of 19): You have two main ways to go: simple declaration with square brackets. const myArray = [] Or instantiation of the Array Object using the constructor method: const myArray = new Array() The trendy kids favor the first way, nowadays, with the empty square brackets, but if you h...
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-avoid-inferring-empty-array-in-typescript
How to Avoid Inferring Empty Array in TypeScript ? - GeeksforGeeks
April 15, 2025 - One common task is declaring empty arrays with specific types, ensuring that your array holds only values of the intended type. These are the following ways to dec · 2 min read How to Return an Empty Promise in TypeScript ? In TypeScript, you can return an empty promise to create an empty ...