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

141
Visualizações
How do I convert an any array to a string array containing other datatypes inside in typescript?

I'm doing a typescript tutorial exercise that wants me to change an any[] array into string[].

// declaring an array of any datatype
const  manufacturers: any[] = [{ id: 'Samsung', checked: false },
        { id: 'Motorola', checked: false },
        { id: 'Apple', checked: false },
        { id: 'Sony', checked: false }
    ];

console.log('Available Products are: ');

 // logic to populate the above declared array's id value
for (const item of manufacturers) {

     console.log(item.id);
    if(item.id === "Apple")
    {
        console.log("check value is " + item.checked)
    }
    }

The above one works, but if I change any[] into string[], it doesn't work. If I do

"const manufacturers: [string,boolean][]=" then it recognizes the boolean and not the string. I'm trying to understand why it doesn't see id as a string variable and make it match. How do I accomplish this without using 'any[]'

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The type of manufacturers is { id: string, checked: boolean }[].

Explanation:

The manufacturers object is an array, containing objects. Each object has an id and a checked property which are of types string and boolean respectively.

So as you said, changing from any[] to string[] won't work, because the manufacturers type is not string[], but { id: string, checked: boolean }[].

const manufacturers: { id: string, checked: boolean }[] = [{ id: 'Samsung', checked: false },
{ id: 'Motorola', checked: false },
{ id: 'Apple', checked: false },
{ id: 'Sony', checked: false }
];

console.log('Available Products are: ');

for (const item of manufacturers) {

  console.log(item.id);
  if (item.id === "Apple") {
    console.log("check value is " + item.checked)
  }
}

As @Calz pointed out, you don't need to explicitly enter the type of the variable, as initialization is taking place at the time of declaration.

Here's a small example explaining this:

let a;
a = 5
console.log(typeof a) // number
a = "string"
console.log(typeof a) // string

let b = 5
b = "some string"; // error as TypeScript blames that type string is not assignable to type number
about 4 years ago · Juan Pablo Isaza 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