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?
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();
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()};
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>