API 调用中的 2 个模型配置有什么区别



我有以下方法在我的服务文件中调用一个api:

getDetail(id: number): Observable<any> {
return this.http.get<any>("api/detail/" + id, { responseType: 'json', observe: 'response' })
}

我很懒,现在只使用any作为模型/接口。

我的问题是两种不同的anys之间有什么区别?在什么情况下,它们可能会有所不同?我有时看到其他人从第二行离开第二个any,即:

return this.http.get("api/detail/" + id, { responseType: 'json', observe: 'response' })

我的问题是两个不同的anys之间有什么区别?

没有,它只是说any是类型any(duuuh,你没有说(

在什么情况下它们可能与另一个

当你想让你的生活更加艰难,并且你想欺骗编译器,使其认为返回类型是给定类型的时

class MyType{public hello(){console.log("hi!")}}
getDetail(id: number): Observable<MyType> {
//turn off type controll
return this.http.get<any>("api/detail/" + id, { responseType: 'json', observe: 'response' })
}
getDetail(5).subscribe(ret=>ret.hello());; //good luck in doing that, but compiler wont complain

最新更新