简体   繁体   English

键入'{类型代码:字符串; }[]' 不能分配给类型 'string[]'。 // Angular 9.1.15, TypeScript

[英]Type '{ typecode: string; }[]' is not assignable to type 'string[]'. // Angular 9.1.15, TypeScript

I do not have good idea about angular, i am learning.我对 angular 不太了解,我正在学习。 Below i have attached my TS file.下面我附上了我的 TS 文件。 I am working on Auto complete search/Type ahead search and i am facing these errors.我正在研究自动完成搜索/提前输入搜索,我正面临这些错误。

<----------------Code--------------------> <----------------代码-------------------------------->

import { Component, OnInit } from '@angular/core';   
import { Title } from '@angular/platform-browser';    
import { FormControl } from '@angular/forms';    
import { Observable } from 'rxjs';    
import { map, startWith } from 'rxjs/operators';    


import { BciImprintComponent, LogoutComponent, ModalWindowService, NavigationService, SidebarNavItem, } from '@bci-web-core/core';


@Component({   
  selector: 'app-root',   
  templateUrl: './app.component.html',   
  styleUrls: ['./app.component.scss']   
})   
export class AppComponent implements OnInit {   
  //options: string[] = ['CytroPac', 'CytroBox', 'GoPakTM'];    
  objectOptions = [   
    { typecode: 'CytroPac' },   
    { typecode: 'CytroBox' },   
    { typecode: 'GoPakTM' }   
  ];   
  myControl = new FormControl();   
  filteredOptions: Observable<string[]>;   

  title = 'Your application';   
  sidebarLinks: SidebarNavItem[] = [];   
  sidebarFooterLinks: SidebarNavItem[] = [   
    {   
      title: 'Maja Mustermann',   
      overlay: {   
        component: LogoutComponent,   
        config: { title: 'Do you want to logout?', onLogout: () => this.logout() }   
      },   
      icon: 'Bosch-Ic-user'   
    },   
    {   
      id: 'imprint',   
      cb: () => this.onAbout(),   
      title: 'Imprint',   
      icon: 'Bosch-Ic-information'   
    }   
  ];   

  constructor(private titleService: Title,   
              private navigationService: NavigationService,   
              private modalWindowService: ModalWindowService) {   
  }   

  ngOnInit() {   
    this.titleService.setTitle(this.title);   
    this.getSidebarLinks().subscribe(sidebarLinks => this.sidebarLinks = sidebarLinks);   
    this.filteredOptions = this.myControl.valueChanges.pipe(   
      startWith(''),   
      map(value => this._filter(value))   
    );   
  }   

  private _filter(value: string): string[] {   
    const filterValue = value.toLowerCase();   
    return this.objectOptions.filter(option =>   
      option.toLowerCase().includes(filterValue)   
    );   
  }      

  getSidebarLinks(): Observable<SidebarNavItem[]> {  
    return this.navigationService.getNavigationItems();   
  }   
 

  onAbout() {   
    this.modalWindowService.openDialogWithComponent(BciImprintComponent);   
  }  
  

}   

<-------------------Error------------------------> <--------------------错误------------>

src/app/app.component.ts:65:5 - error TS2322: Type '{ typecode: string; src/app/app.component.ts:65:5 - 错误 TS2322: Type '{ typecode: string; }[]' is not assignable to type 'string[]'. }[]' 不能分配给类型 'string[]'。 Type '{ typecode: string;键入'{类型代码:字符串; }' is not assignable to type 'string'. }' 不可分配给类型“字符串”。

 return this.objectOptions.filter(option =>
   
  option.toLowerCase().includes(filterValue)

 );

src/app/app.component.ts:66:14 - error TS2339: Property 'toLowerCase' does not exist on type '{ typecode: string; src/app/app.component.ts:66:14 - 错误 TS2339: 类型 '{ typecode: string; 上不存在属性'toLowerCase' }'. }'。

 option.toLowerCase().includes(filterValue)
      

Based on the type definitions, I'd guess you're passing a string and want to filter the array of objects based on it.根据类型定义,我猜您正在传递一个string并希望根据它过滤对象数组。 You'd then need to return array of objects instead of array of strings.然后,您需要返回对象数组而不是字符串数组。

You could use Array#map with Array#filter .您可以将Array#mapArray#filter一起使用。 Try the following尝试以下

private _filter(value: string): string[] {   
  const filterValue = value.toLowerCase();
  return this.objectOptions
    .map(objectOption => objectOption.option) // produces ['CytroPac', 'CytroBox', 'GoPakTM']
    .filter(option => option.toLowerCase().includes(filterValue))
    .map(option => ({ typecode: option }));   // map back to array of objects
}      

Or to use your existing implementation of the _filter function, you could use the commented out options variable (array of strings) instead of the array of objects objectOptions .或者要使用_filter function 的现有实现,您可以使用注释掉的options变量(字符串数组)而不是对象数组objectOptions

Update: using only Array#filter更新:仅使用Array#filter

See the comment by @Random below to use only the Array#filter without the need for redundant Array#map s.请参阅下面@Random 的评论,仅使用Array#filter而无需多余的Array#map

暂无
暂无

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

相关问题 类型“null”不能分配给类型“string”。 角/打字稿 - Type 'null' is not assignable to type 'string'. Angular/TypeScript 角度打字稿错误:“字符串”类型无法分配给“任何[]”类型 - Angular typescript error: Type 'string' is not assignable to type 'any[]' 类型“事件”不可分配给类型“字符串”。 angular typescript 错误 - Type 'Event' is not assignable to type 'string'. angular typescript error 当使用 Angular @Input 装饰器时,Typescript Type 'string' is not assignable to type - When using Angular @Input decorator, Typescript Type 'string' is not assignable to type 带有 Typescript 的 Momentjs:“类型字符串不可分配给类型 &#39;Date&#39;” - Momentjs with Typescript : “type string is not assignable to type 'Date'” 键入'字符串 | string[]' 不可分配给类型 'string' - typescript 类型错误 - Type 'string | string[]' is not assignable to type 'string' - typescript type error 类型字符串不可分配给“从不”Angular - Type String is not assignable to 'never' Angular Typescript - 类型“字符串”不可分配给类型“布尔” - Typescript - Type 'string' is not assignable to type 'boolean' 打字稿类型 &#39;string | 留言 | (string[] | Message[])[]&#39; 不可分配给类型 &#39;string[] | 信息[]&#39; - Typescript Type 'string | Message | (string[] | Message[])[]' is not assignable to type 'string[] | Message[]' Angular - 类型'string'不能赋值为'boolean' - Angular - Type 'string' is not assignable to type 'boolean'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM