【发布时间】: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: number或id: 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