Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

183
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda