TypeScript: creating an empty container array

I am creating a simple logic game called "Three Crimes" in TypeScript.

When trying to pre-allocate a typed array in TypeScript, I tried to do something like this:

var arr = Criminal[]; 

which gave the error "Check the expression format of the expression."

also tried to do it

 var arr : Criminal = []; 

and it produced "cannot turn any [] into a" Crime "

What is the 'TypeScript' way for this?

+55
javascript typescript
Apr 05 '13 at 5:43
source share
3 answers

The problem of correctly pre-allocating a typed array in TypeScript was somewhat obscured by the array literal syntax, so it was not as intuitive as I thought.

The right way:

 var arr : Criminal[] = []; 

This will give you a properly printed empty array stored in the variable 'arr'

Hope this helps others!

+99
Apr 05 '13 at 5:43
source share

Existing answers missed the option, so the full list is:

 // 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>(); 
  • An explicit type declaration is a general solution if whenever type inference is not performed to declare a variable.

  • The advantage of using a type assertion (sometimes called cast, but this does not distinguish a listing in TypeScript) for any expression, so it can be used even if no variable is declared. There are two syntax for type assertions, but only the latter will work in conjunction with JSX if you do not notice it.

  • Using the Array constructor is something that will help you in this particular use case, but which I personally consider the most readable. However, at runtime there is a slight performance impact . Also, if someone was crazy enough to override the Array constructor, the value might change .

This is a matter of personal preference, but I think the third option is the most readable. In the vast majority of cases, the aforementioned deficiencies would be minor, and readability is the most important factor.

*: funny fact; at the time of this writing, the performance difference was 60% in Chrome, and there was no noticeable difference in performance in Firefox.

+22
Apr 05 '17 at 12:10
source share

I know this is an old question, but recently I ran into a similar problem that could not be solved in this way, since I had to return an empty array of a certain type.

I had

 return []; 

where [] was Criminal[] .

Neither for me nor for return: Criminal[] []; , nor for return []: Criminal[]; .

At first glance, I solved this by creating a typed variable (as you correctly reported) before returning it, but (I don’t know how JavaScript engines work), it can create overhead and are less readable.

For thoroughness, I will also report this decision in my answer:

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

In the end, I found the TypeScript type casting, which allowed me to solve the problem in a more concise and readable (and possibly efficient) way:

 return <Criminal[]>[]; 

Hope this helps future readers!

+12
Aug 09 '16 at 8:46
source share



All Articles