简体   繁体   English

Typescript:“员工 []”类型上不存在属性“名称”

[英]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?我对 typescript 完全陌生,我被这个错误消息难住了: Property 'name' does not exist on type 'Employee[]'有人可以指出我没有在Employee中应用“name”类型的地方吗大批? 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));

Try using find instead of filter.尝试使用 find 而不是 filter。 Filter returns an array.过滤器返回一个数组。 Find returns a single object. Find 返回单个 object。 Next time, if using vscode, hover over employee on the first line of fetchEmployeeName, and check its type.下一次,如果使用 vscode,则 hover 在 fetchEmployeeName 的第一行覆盖employee,并检查其类型。 Intellisense in vscode will point out to you that employee is clearly an array. vscode 中的 Intellisense 会向你指出,employee 显然是一个数组。

filter returns a new array containing all matching items: filter返回一个包含所有匹配项的新数组:

[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 .您要使用的是find ,它将返回单个匹配项或undefined

Modify the fetchEmployeeName function to use find :修改fetchEmployeeName function 以使用find

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

  if (employee === undefined) return null;

  return employee.name;
}

there you have it, so what happened, your filter is returning to a new "Employee" that is not defined as an object,my advise is to always try to use pure functions and understand what your return is有了它,发生了什么,您的过滤器正在返回一个未定义为 object 的新“员工”,我的建议是始终尝试使用纯函数并了解您的回报是什么

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, employees: Employee[]){
      let employee = null
      for (let i = 0, j = employees.length ; i < j; i++) {
          if (employees[i].id === id) {
              employee = employees[i].name 
          }
      }
      return employee
  }

  console.log(`Got employee 3: ${fetchEmployeeName(3,employees)}`);

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).我强烈建议你使用find而不是filter ,但如果你真的想坚持你的方法,你将不得不通过它的索引访问 employees 数组中唯一的成员( filter返回一个填充了满足指定元素的数组健康)状况)。 EG:例如:

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).同样,您可以通过使用filter来解决这个特定问题,因为它返回您访问的单个元素而无需索引(这将允许您保留 return 语句原样)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM