简体   繁体   English

Angular 管道上的 .includes 返回“属性 'includes' 在类型 'never'.ts(2339)' 上不存在错误消息

[英].includes on Angular pipe is returning 'Property 'includes' does not exist on type 'never'.ts(2339)' error message

I'm coding an Angular Pipe that has to iterate an array of objects and filter by field and field value.我正在编写一个 Angular Pipe,它必须迭代一个对象数组并按字段和字段值进行过滤。

When I'm checking if the value field contains the value for what is filtered by i get the following error message: Property 'includes' does not exist on type 'never'.ts(2339)当我检查 value 字段是否包含被过滤的值时,我收到以下错误消息: Property 'includes' does not exist on type 'never'.ts(2339)

This is my pipe.ts code:这是我的 pipe.ts 代码:

transform(items: [], field: string, value: string): any[] {

    let res = [];

   console.log(items);

    for(let a = 0; a < items.length; a++) {

      if((items[a][field]).includes(value)) {
        res.push(items[a]);
      }

    }

    return res;
  }

What I want is that when I pass some array through the pipe it check if the atribute contains the value specified.我想要的是,当我通过管道传递一些数组时,它会检查属性是否包含指定的值。 The array, the atribute and the filter value are setted on the view.数组、属性和过滤器值在视图上设置。 Ex: <p>{{ foods | filter : 'name' : 'zz' }}</p>例如: <p>{{ foods | filter : 'name' : 'zz' }}</p> <p>{{ foods | filter : 'name' : 'zz' }}</p>

This is the array I have on the controller:这是我在控制器上的数组:

foods: Object[] = [
    {
      name: "Pizza",
      calories: 400,
      image: "https://i.imgur.com/eTmWoAN.png",
      quantity: 0
    },
    {
      name: "Salad",
      calories: 150,
      image: "https://i.imgur.com/DupGBz5.jpg",
      quantity: 0
    },
    {
      name: "Sweet Potato",
      calories: 120,
      image: "https://i.imgur.com/hGraGyR.jpg",
      quantity: 0
    },
    {
      name: "Gnocchi",
      calories: 500,
      image: "https://i.imgur.com/93ekwW0.jpg",
      quantity: 0
    },
    {
      name: "Pot Roast",
      calories: 350,
      image: "https://i.imgur.com/WCzJDWz.jpg",
      quantity: 0
    },
    {
      name: "Lasagna",
      calories: 750,
      image: "https://i.imgur.com/ClxOafl.jpg",
      quantity: 0
    },
    {
      name: "Hamburger",
      calories: 400,
      image: "https://i.imgur.com/LoG39wK.jpg",
      quantity: 0
    },
    {
      name: "Pad Thai",
      calories: 475,
      image: "https://i.imgur.com/5ktcSzF.jpg",
      quantity: 0
    },
    {
      name: "Almonds",
      calories: 75,
      image: "https://i.imgur.com/JRp4Ksx.jpg",
      quantity: 0
    },
    {
      name: "Bacon",
      calories: 175,
      image: "https://i.imgur.com/7GlqDsG.jpg",
      quantity: 0
    },
    {
      name: "Hot Dog",
      calories: 275,
      image: "https://i.imgur.com/QqVHdRu.jpg",
      quantity: 0
    },
    {
      name: "Chocolate Cake",
      calories: 490,
      image: "https://i.imgur.com/yrgzA9x.jpg",
      quantity: 0
    },
    {
      name: "Wheat Bread",
      calories: 175,
      image: "https://i.imgur.com/TsWzMfM.jpg",
      quantity: 0
    },
    {
      name: "Orange",
      calories: 85,
      image: "https://i.imgur.com/abKGOcv.jpg",
      quantity: 0
    },
    {
      name: "Banana",
      calories: 175,
      image: "https://i.imgur.com/BMdJhu5.jpg",
      quantity: 0
    },
    {
      name: "Yogurt",
      calories: 125,
      image: "https://i.imgur.com/URhdrAm.png",
      quantity: 0
    }
  ]

I don't get why the .contains throws error if I do console.log(items[a][field] inside the loop I get the food's name correctly.如果我在循环中执行console.log(items[a][field]我不明白为什么 .contains 会抛出错误,我正确地得到了食物的名称。

Consider the line transform(items: [], field: string, value: string): any[] { specifically items: [] This indicates that the type of item is an empty array hence the error考虑行transform(items: [], field: string, value: string): any[] {特别是items: []这表明 item 的类型是一个空数组,因此错误

To resolve this simply provide a type, you can start by item: any[]要解决这个问题,只需提供一个类型,您可以从item: any[]开始item: any[]

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

相关问题 TS2339:“字符串”类型上不存在属性“包含” - TS2339: Property 'includes' does not exist on type 'string' Typescript 错误:属性“status”在类型“never”上不存在。ts(2339) - Typescript error: Property 'status' does not exist on type 'never'.ts(2339) “TypedResponse”类型上不存在属性<never> '.ts(2339)</never> - Property does not exist on type 'TypedResponse<never>'.ts(2339) 类型“从不”上不存在属性“单击”。 TS2339 - Property 'click' does not exist on type 'never'. TS2339 NextJs 中的 axios 响应中的类型“never”.ts(2339) 上不存在属性 - Property does not exist on type 'never'.ts(2339) in axios response in NextJs TypeScript 错误:属性“scrollIntoView”在类型“never”上不存在。 TS2339 - TypeScript error: Property 'scrollIntoView' does not exist on type 'never'. TS2339 错误TS2339:类型“字符串”上不存在属性“默认”。** - Error TS2339: Property 'Default' does not exist on type 'string'.** 错误 TS2339:类型“CustomerTransfert[]”上不存在属性“num” - error TS2339: Property 'num' does not exist on type 'CustomerTransfert[]' 错误TS2339:类型“ HTMLElement”上不存在属性“ imageID” - error TS2339: Property 'imageID' does not exist on type 'HTMLElement' 错误TS:2339类型{}上没有属性“ Project” - error TS: 2339 Property “Project” does not exist on type { }
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM