How do I initialize an empty typed array in TypeScript?

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’.” What is the correct way to initialize a TypeScript empty array?

Here’s a complete list of options for initializing a typed array in TypeScript:

Explicitly declare the type:

var arr: Criminal[] = [];
Via type assertion:

Via type assertion:

var arr = <Criminal[]>[];
var arr = [] as Criminal[];

Using the Array constructor:

var arr = new Array<Criminal>();

Explicitly specifying the type is a solid, general approach when type inference doesn’t quite get you what you need. It’s clear and straightforward, but sometimes might feel a bit verbose.

Type assertions (sometimes called “casts,” though they’re not true casts in TypeScript) give you flexibility. They can be applied to any expression, even without declaring a variable. The two assertion syntaxes are interchangeable, except if you’re working with JSX, in which case you’ll want to use the as syntax.

Now, I personally favor using the Array constructor. It feels cleaner to me, and the syntax is a bit more compact. That said, there’s a minor runtime performance hit to be aware of, and, in rare cases, if someone redefines the Array constructor, you might get unexpected behavior. But honestly, in most scenarios, the impact is minimal, and I think the added readability makes it worth it.

Fun fact: When I last checked, the performance difference was about 60% in Chrome, while Firefox didn’t show any measurable difference. But unless you’re dealing with performance-critical code, the readability and clarity usually take priority!

I recently faced a similar issue where I needed to return an empty array of a specific type, but the usual approaches didn’t work for me. Initially, I tried:

return [];

This was supposed to return an array of type Criminal[], but it didn’t work as expected. I also tried variations like:

return: Criminal[] [];
return []: Criminal[];

Neither of these worked either.

At first, I solved it by declaring a typed variable before returning it, similar to what you mentioned:

let temp: Criminal[] = [];
return temp;

While this approach works, it feels a bit verbose and could introduce unnecessary overhead.

Later, I found a more concise and efficient way to handle it using TypeScript type casting:

return <Criminal[]>[];

This method keeps things simple and clean. Hopefully, this helps others who run into the same issue!

The correct way to declare an empty array of a specific type in TypeScript is:


var arr: Criminal[] = [];

If you’re using var, this implies the declaration is within a function. I recommend using let instead of var for better scoping and to avoid potential issues:

let arr: Criminal[] = [];

If you’re declaring it as a class property, use access modifiers such as private, public, or protected:

class Example {
    private arr: Criminal[] = [];
}