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

115
Visualizações
Typescript: Property 'name' does not exist on type 'Employee[]'

I am completely new to typescript, and I'm stumped by this error message: Property 'name' does not exist on type 'Employee[]' Could someone please point out where I'm not applying the "name" type within the Employee array? Thanks.

interface Employee {
  id: number;
  name: string;
  title: string;
}

var employees: Employee[] = [
  { id: 0, name: "Franklin", title: "Software Enginner" },
  { id: 1, name: "Jamie", title: "Human Resources" },
  { id: 2, name: "Henry", title: "Application Designer" },
  { id: 3, name: "Lauren" title: "Software Enginner" },
  { id: 4, name: "Daniel" title: "Software Enginner 2" },
];

function fetchEmployeeName(id : number) {
  var employee = employees.filter(
    (employee) => employee.id === id
  );

// The error occurs when I try to return a "name" under an employee that matched by id.
  return employee.name;
}

console.log("Got Employee: "), fetchEmployeeName(3));
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

filter returns a new array containing all matching items:

[1, 2, 3].filter(i => i === 4)

The above will return an empty array.

What you want to use is find, which will return a single matching item or undefined.

Modify the fetchEmployeeName function to use find:

function fetchEmployeeName(id : number): string | null {
  var employee = employees.find(
    (employee) => employee.id === id
  );

  if (employee === undefined) return null;

  return employee.name;
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Try using find instead of filter. Filter returns an array. Find returns a single object. Next time, if using vscode, hover over employee on the first line of fetchEmployeeName, and check its type. Intellisense in vscode will point out to you that employee is clearly an array.

about 4 years ago · Juan Pablo Isaza Relatório

0

I highly recommend you to use find instead of filter, but if you really want to stick to your approach, you will have to access the only member in the employees array though its index (filter returns an array filled with the elements that meet the specified condition). E.G.:

return employee[0].name

Again, you can solve this particular issue by using filter, since it returns a single element you access without the need of an index (this will allow you to leave the return statement as it is).

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