简体   繁体   English

类型“any [] | undefined”不能分配给类型“any []”

[英]the type "any [] | undefined" cannot be assigned to the type "any []"

i have a new problem since i updated today my npm and all modules.我有一个新问题,因为我今天更新了我的 npm 和所有模块。 Everything worked before i did the update.在我进行更新之前一切正常。

error is "the type "any [] |错误是“类型”任何 [] | undefined" cannot be assigned to the type "any []"" undefined" 不能分配给类型 "any []""

code代码

    this.myservice.myfunction().toPromise().then((data)=>{
      this.variable = data;
    }).catch((error)=>{
      console.log("Promise rejected with " + JSON.stringify(error));
    });

and in the service:并在服务中:

  myfunction(): Observable<any[]> {
    return this.http.post<any[]>(`${this.baseUrl}/GetDataFromBackend`, {  }).pipe(
      map((res) => res),
      catchError(this.handleError));
  }

anyone an idea why i got this error after the update?有人知道为什么我在更新后收到此错误吗? How can i fix this?我怎样才能解决这个问题? i got this at nearly every component and backend connection...我几乎在每个组件和后端连接都得到了这个......

This is a typescript error.这是 typescript 错误。 You can overcome the problem in several ways.您可以通过多种方式克服该问题。

1: You can check for undefined like this: 1:您可以像这样检查未定义:

    this.myservice.myfunction().toPromise().then((data)=>{
       if(data !== undefined && data !== null) {
          this.variable = data;
      }
    }).catch((error)=>{
      console.log("Promise rejected with " + JSON.stringify(error));
    });

2-if you know the value will never be undefined you can force it with ":" like this: 2-如果您知道该值永远不会未定义,则可以使用“:”强制它,如下所示:

    this.myservice.myfunction().toPromise().then((data)=>{
       
          this.variable = data!;
      
    }).catch((error)=>{
      console.log("Promise rejected with " + JSON.stringify(error));
    });

3- you can change the type of the variable when you declare it to be like: 3-您可以在声明变量时更改变量的类型:

let variable: any [] | undefined = [];

I am sure there is a couple more ways to fix this issue.我相信还有更多的方法可以解决这个问题。 But these 3 are the most commonly used in my personal experience.但是这三个是我个人经验中最常用的。

With the advantage that you dont have to disable nice features like "strict" globally in your project.其优点是您不必在项目中全局禁用“严格”等不错的功能。

The point is to develop using a stricter strongly typed model and not to weaken your code by "lowering the bar" on the typescript.重点是使用更严格的强类型 model 进行开发,而不是通过在 typescript 上“降低标准”来削弱您的代码。

暂无
暂无

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

相关问题 void 类型的 Function 不能分配给 Observable 类型<any></any> - Function of type void can't be assigned to type Observable<any> 类型“ Observable &lt;{} []&gt;”无法转换为类型“ AngularFireList” <any[]> “ - Type 'Observable<{}[]>' cannot be converted to type 'AngularFireList<any[]>' 未定义未分配给ComponentFactory <any> &#39;DynamicComponent&#39; - undefined is not assigned to ComponentFactory<any> 'DynamicComponent' 类型“AA”不可分配给类型“NgIterable”<any> | 空 | 在 Angular 中未定义 - Type 'AA' is not assignable to type 'NgIterable<any> | null | undefined' in Angular 类型'可观察的<client[]> ' 不可分配给类型 'NgIterable<any> | null | 不明确的'</any></client[]> - Type 'Observable<client[]>' is not assignable to type 'NgIterable<any> | null | undefined' : &#39;ItmesModel[] 类型的参数 | undefined&#39; 不能分配给类型为 &#39;any[]&#39; 的参数 - : Argument of type 'ItmesModel[] | undefined' is not assignable to parameter of type 'any[]' 键入“抽象控件 | null' 不可分配给类型 'NgIterable<any> | null | 不明确的'</any> - Type 'AbstractControl | null' is not assignable to type 'NgIterable<any> | null | undefined' 类型“对象”不可分配给类型“NgIterable”<any> | null | 不明确的'</any> - Type 'object' is not assignable to type 'NgIterable<any> | null | undefined' 类型“对象”不可分配给类型“NgIterable”<any> | null | 不明确的'。 在 Angular 11</any> - Type 'Object' is not assignable to type 'NgIterable<any> | null | undefined'. in Angular 11 类型“对象”不可分配给类型“NgIterable”<any> | null | 不明确的'。 在 Angular</any> - Type 'Object' is not assignable to type 'NgIterable<any> | null | undefined'. in Angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM