简体   繁体   English

字符串类型的参数 | null 不可分配给类型字符串错误的参数

[英]argument of type string | null is not assignable to parameter of type string error

When I try to get local storage records It gives following error.当我尝试获取本地存储记录时,它会出现以下错误。

"Argument of type 'string | null' is not assignable to parameter of type 'string'.\n  Type 'null' is not assignable to type 'string'.",

here is the code这是代码

this.service.getAllPets(this.CatDog).subscribe(
  data => {
    this.pets = data;
    console.log(data);
    // tslint:disable-next-line:no-bitwise
    const newPet = JSON.parse(localStorage.getItem('newPet'));
    if (newPet){
      this.pets = [newPet, ...this.pets];
    }
    console.log(this.route.snapshot.url.toString());
  }, error => {
    console.log('httperror:');
    console.log(error);
  }
);

JSON.parse accepts an arg of type string JSON.parse接受字符串类型的 arg

localStorage.getItem returns an arg of type string or null (if its not found) localStorage.getItem返回字符串类型的 arg 或 null(如果未找到)

therefore you can add a quick default value set before the parse.因此,您可以在解析之前添加一个快速的默认值集。 like so:像这样:

const newPet = JSON.parse(localStorage.getItem('newPet') || "{}");

another option is to fetch the result from the localstorage, check that it is not null and then parse it.另一种选择是从本地存储中获取结果,检查它是否不是 null 然后解析它。

暂无
暂无

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

相关问题 Typescript - 'string | 类型的参数 (string | null)[]' 不能分配给“string”类型的参数 - Typescript - Argument of type 'string | (string | null)[]' is not assignable to parameter of type 'string' 'string | 类型的参数 null' 不能分配给“字符串”类型的参数。 类型 'null' 不能分配给类型 'string' - Argument of type 'string | null' is not assignable to parameter of type 'string'. Type 'null' is not assignable to type 'string' 'string | 类型的参数 null' 不能分配给类型为 'string' 的参数。 类型 'null' 不能分配给类型 'string'。 角 12 - Argument of type 'string | null' is not assignable to parameter of type 'string'. Type 'null' is not assignable to type 'string'. Angular 12 'string | 类型的参数 null' 不能分配给类型为 'string' 的参数 - Argument of type 'string | null' is not assignable to parameter of type 'string' React,typescript - 'string | 类型的参数 null' 不能分配给“字符串”类型的参数 - React, typescript - Argument of type 'string | null' is not assignable to parameter of type 'string' 'string | 类型的参数 null' 不可分配给类型为“string |”的参数不明确的' - Argument of type 'string | null' is not assignable to parameter of type 'string | undefined' useState 和 localStorage:“string null”类型的参数不可分配给“string”类型的参数 - useState and localStorage: argument of type 'string null' is not assignable to parameter of type 'string' 错误:“‘string | string[]’类型的参数不可分配给‘string’类型的参数” - Error : " Argument of type 'string | string[]' is not assignable to parameter of type 'string' " “eventKey”类型的参数不可分配给“字符串”类型的参数。 类型 'null' 不能分配给类型 'string' - Argument of type 'eventKey' is not assignable to parameter of type 'string'. Type 'null' is not assignable to type 'string' 类型'字符串|的参数 undefined'不能赋予类型'“无标题”|参数 null | 未定义” - Argument of type 'string | undefined' is not assignable to parameter of type '“untitled” | null | undefined'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM