简体   繁体   English

类型 'Object' 缺少类型 'any[]' 的以下属性:length、pop、push、concat 和 26 more.ts

[英]Type 'Object' is missing the following properties from type 'any[]': length, pop, push, concat, and 26 more.ts

I got error in visual studio code ->我在 Visual Studio 代码中遇到错误 ->

Type 'Object' is missing the following properties from type 'any[]': length, pop, push, concat, and 26 more.ts类型 'Object' 缺少类型 'any[]' 的以下属性:length、pop、push、concat 和 26 more.ts

I dont know reaseon but check my code:我不知道原因,但请检查我的代码:

  trainingPlanResponse: any[] = [];


  this.service.postToGetData(model).subscribe(
    data => {
      // this.trainingPlanResponse.push(data);
      this.trainingPlanResponse = data; // HERE IS ERROR!!!
    },
    err => {
      console.log(err) 
    }
  )

When i set to当我设置为

trainingPlanResponse: any; 

This work but I need to set in html这项工作,但我需要在 html 中设置

        <div *ngIf="trainingPlanResponse.length > 0">
            response 
        </div>

Important: I don't have model interface for this!重要提示:我没有用于此的模型界面!

A response from an http request returns an Observable of type Object来自 http 请求的响应返回Object类型的 Observable

Example例子

Lets say you have a function postToGetData()假设您有一个函数postToGetData()

postToGetData() {
  return this.httpClient.get('my-url')
}

Typescript will infer the function postToGetData to return an Observable<Object> Typescript将推断函数postToGetData返回一个Observable<Object>

The easiest solution to this is to simply use type casting like below最简单的解决方案是简单地使用类型转换如下

postToGetData() {
  return this.httpClient.get<any[]>('my-url')
}

The above will infer postToGetData to return any[]以上将推断postToGetData返回any[]

You may also define the return type of the function as any[]您还可以将函数的返回类型定义为any[]

postToGetData(): any[] {
  return this.httpClient.get('my-url').pipe(map(items => items as any[]))
}

暂无
暂无

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

相关问题 React / TypeScript - 类型“{ index:number}”缺少类型“IList”的以下属性:length、pop、push、concat 和另外 26 个 - React / TypeScript - Type '{ index:number}' is missing the following properties from type 'IList': length, pop, push, concat, and 26 more “IProduct”类型缺少“IProduct[]”类型的以下属性:length、pop、push、concat 等 29 个 - Type 'IProduct' is missing the following properties from type 'IProduct[]': length, pop, push, concat, and 29 more 输入“基本响应”<IavailableParameters[]> &#39; 缺少类型 &#39;IavailableParameters[]&#39; 中的以下属性:长度、弹出等 - Type 'BaseResponse<IavailableParameters[]>' is missing the following properties from type 'IavailableParameters[]': length, pop, etc TS 错误:“事件”类型缺少“键盘事件”类型中的以下属性 - TS error: Type 'event' is missing the following properties from type 'keyboardevent' TS2739 类型“any[]”缺少来自“JSON”类型的以下属性:解析、字符串化、[Symbol.toStringTag] - TS2739 Type 'any[]' is missing the following properties from type 'JSON': parse, stringify, [Symbol.toStringTag] 错误 TS2739:类型“{}”缺少类型中的以下属性 - error TS2739: Type '{}' is missing the following properties from type TS2740 缺少 type、Typescript、NestJS 中的以下属性 - TS2740 is missing the following properties from type, Typescript, NestJS “订阅”类型缺少“可观察”类型的以下属性<any> ': _isScalar, source, operator, lift, and 6 more</any> - Type 'Subscription' is missing the following properties from type 'Observable<any>': _isScalar, source, operator, lift, and 6 more 类型缺少“架构”类型的以下属性<any, Model<any,any,any,any> , {}, {}&gt;&#39;:添加、childSchemas、clearIndexes、克隆等 37 个 - Type is missing the following properties from type 'Schema<any, Model<any,any,any,any>, {}, {}>': add, childSchemas, clearIndexes, clone, and 37 more 类型“Element”缺少类型“OfferProps”中的以下属性:id、firstName、city、price 和另外 2 个。 TS2740 - Type 'Element' is missing the following properties from type 'OfferProps': id, firstName, city, price, and 2 more. TS2740
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM