简体   繁体   English

. 为什么会出现以下错误? 类型“照片”不可分配给类型“从不”

[英]. WHY DO I GET THE FOLLOWING ERROR? Type 'Photo' is not assignable to type 'never'

enter image description here在此处输入图像描述

export class PhotoListComponent implements OnInit {

  photos = [];

  constructor(private photoService: PhotoService) { }

  ngOnInit() {
    this.photoService.getPhotos()
    .subscribe(
      res => {
        this.photos = res;
      },
      err => console.log(err)
    )
  }

}
photos = [];

Since you havn't specified a type here, typescript needs to infer it.由于您没有在此处指定类型,因此 typescript 需要推断它。 But with no clues about what this array is supposed to contain, typescript infers the type to be never[] .但是没有关于这个数组应该包含什么的线索, typescript 推断类型为never[] Then later on, when you try to assign something else to it, you get a type error.稍后,当您尝试为其分配其他内容时,您会收到类型错误。

Change it to:将其更改为:

photos: Photo[] = [];

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

相关问题 为什么我会收到错误 Type '“x”' is not assignable to type “type Enum” - Why do I get the error Type '“x”' is not assignable to type “type Enum” “数字”类型的参数不可分配给“从不”类型的参数,为什么我会收到此错误? - Argument of type 'number' is not assignable to parameter of type 'never' why i am getting this error? Angular 错误类型字符串不可分配给类型 never - Angular error Type string is not assignable to type never 类型 'any[]' 不可分配给类型 'never' - Type 'any[]' is not assignable to type 'never' 我收到错误消息:无法将类型“ Subscription”分配给类型“ Observable” - I get Error: Type 'Subscription' is not assignable to type 'Observable' 在BehaviorSubject实例化期间,Typescript永远不会输入错误(从不[]不可分配) - Typescript never type error during BehaviorSubject instantiation (never[] is not assignable) 输入“可观察的<never> ' 不可分配给类型 'never'</never> - Type 'Observable<never>' is not assignable to type 'never' 类型字符串不可分配给“从不”Angular - Type String is not assignable to 'never' Angular “any”类型的参数不能分配给“never”类型的参数? - Argument of type 'any' is not assignable to parameter of type 'never'? 类型参数<interface>不可分配给“从不”类型的参数</interface> - Argument of type <Interface> is not assignable to parameter of type 'never'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM