【问题标题】:Parameter '...' implicitly has an 'any' type参数“...”隐式具有“任何”类型
【发布时间】:2021-06-09 14:10:42
【问题描述】:

我正在为我的项目使用 VS Code,但我遇到了这个错误。我检查了其他解决方案,例如设置 "noImplicitAny: false" 但问题 - “Parameter 'res' 隐含了 'any' 类型” & “Parameter 'id ' 隐含地有一个 'any' 类型”仍然存在。 TypeScript 代码如下。


  getContacts()
  {
    return this.http.get('http://localhost:3000/api/contacts')
    .map(res => res.json());
  }

  //add contact method
  addContact (newContact)
  {
    var headers = new Headers();
    headers.append('content-Type', 'application/json');
    return this.http.post('http://localhost:3000/api/contact', newContact, {headers:headers})
      .map(res => res.json());
  }

  //delete method
  deleteContact(id)
  {
    return this.http.delete('http://localhost:3000/api/contact/'+id)
      .map(res => res.json());
  }

【问题讨论】:

  • 为什么不自己注释函数参数,比如id: numberid: string?另外,请考虑修改此代码,以构成一个适合放入独立 IDE(如 TypeScript Playground)的 minimal reproducible example,您正在讨论的问题存在而其他问题不存在。 Link
  • this.http的类型是什么?如果我使用import {Http} from "@angular/http"; 类型,那么我实际上会在.map 上得到错误,因为http.get 等返回Observable<Request>。你的函数期待Array<Request>,感觉它不可能是正确的。为什么是array
  • 至于“参数 'id' 隐含地具有 'any' 类型。”,这是一个完全预期和期望的错误。您需要声明 id 参数的类型。 deleteContact(id: string)

标签: angularjs typescript


【解决方案1】:

也许尝试将res => 更改为(res: any) =>

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 2018-05-30
  • 2021-07-20
  • 2020-02-20
  • 1970-01-01
  • 2023-03-08
  • 2020-02-02
  • 2019-11-21
  • 2021-03-29
  • 1970-01-01
相关资源
最近更新 更多