Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

182
Views
Question about array of types in Typescript

When looking up how to declare arrays of types online I found the following:

arrayVar: Array<Type>

Straight forward, so I tried declaring my variable as follows:

transactions: Transactions = { total : 0, list: Array<Transaction>};

This produces a syntax error, therefore I used the following instead:

transactions: Transactions = { total : 0, list: Array<Transaction>()};

Which compiles and works as expected. My questions is what purpose do the parenthesis serve and why does it no compile without them in my declaration?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

When you are working with class X and you call X() you are calling the constructor method of that class. Since you are trying to create a new array you need to call Array() but with a <type> indicator, and that initializes your list element.

If you don't like that syntax you could use square brackets

transactions: Transactions = { total : 0, list: Transaction[]};

Also it seems that your transactions is nothing more that an array, if you need the total just count the number of items in the array.

transactions: Transaction[] ;
transactions.push(transA) ;
transactions.push(transB) ;
let total = transactions.length();
over 4 years ago · Santiago Trujillo Report

0

The parentheses signify a function call, you are instantiating a new empty array. It conforms to your type definition because it has no members.

In this declaration:


transactions: Transactions = { total : 0, list: Array<Transaction>};

You are passing the Array function instead. This does not meet the type definition of list, which is an Array instance.

FYI, you have declared the type of the assigned object to be the Transactions type, list will already be inferred as being an array of the Transaction type if you have defined it there, so you don't need to pass the member type to Array.


transactions: Transactions = { total : 0, list: Array()};

over 4 years ago · Santiago Trujillo Report

0

Consider this

export type Transaction={};
export type Transactions={
  total:number;
  list:Transaction[]; //or Array<Transaction>
}

//what you did - and what is fine

let transactions: Transactions = {total:0,list:[]}; //empty array

//what you tried to do and it failed
let badTx: Transactions={total:0,list:Transaction[]}; //type `Transaction[]`

as you can clearly see, you tired to assign a TYPE of Transaction[] while array (of such type) was expected. Therefore the error.

Parenthesis from the question are just part of constructor invocation and would be equal to new Array<Transaction>

Check it out on TS Playground

Long story short - Array is a type while Array<x>() is newly created instance of Array<x>

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!