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

205
Visualizações
How to predefine object with element as an array <string> type?

I have an object, and i need to define in this object sub element, which will be as an array. But how do i define it's type? Without type definition it throws error.

Implicitly has any[]

let someObject = {
  somePropety: {
    A : 0,
    B : 0,
    C : []<string>,
  }
}

Does anyone know how to asign C element an array of string type ?

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

0

You're looking for string[]. However, it doesn't look like your example is a type definition?

I would write it like this:

let someObject: { somePropety: { A: number; B: number; C: string[] } } = {
        somePropety: {
          A: 0,
          B: 0,
          C: []
        }
}; 
about 4 years ago · Juan Pablo Isaza Relatório

0

You seem to be looking for

let someObject = {
  someProperty: {
    A: 0,
    B: 0,
    C: [] as string[],
  },
};

Alternatively, you can declare the type of the entire someObject variable (see @wooooooo's or @ksav's answers for that) instead of using a type assertion on the empty array literal.

about 4 years ago · Juan Pablo Isaza Relatório

0

If you are trying to define C as an array of strings, you can do:

C: string[];

or using a generic array type:

C: Array<string>

You can find more detail on TypeScript basic types here

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