类型“{}"上不存在属性“json" [英] Property 'json' does not exist on type '{}'
本文介绍了类型“{}"上不存在属性“json"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 Typescript 中有一个抽象基类,如下所示:
I have an abstract base class in Typescript that looks like this:
import {Http, Headers, Response} from 'angular2/http';
export abstract class SomeService {
constructor(private http:Http) {}
protected post(path:string, data:Object) {
let stringifiedData = JSON.stringify(data);
let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Accept', 'application/json');
this.http.post(`http://api.example.com/${path}`, stringifiedData, { headers })
.map(res => res.json())
.subscribe(obj => console.log(obj));
}
}
它完美地工作.然而,Typescript 编译器抱怨 .map(res => res.json())
.我不断收到此错误:
It works perfectly. However, the Typescript compiler is complaining about .map(res => res.json())
. I keep getting this error:
ERROR in ./src/app/components/shared/something/some.abstract.service.ts
(13,29): error TS2339: Property 'json' does not exist on type '{}'.
我遵循了angular 2 文档中的示例,它有效.我只是厌倦了盯着这个错误.我错过了什么吗?
I followed the examples in the angular 2 documentation, and it works. I'm just sick of staring at this error. Am I missing something?
推荐答案
对我来说这看起来很奇怪...
To me this looks strange...
.map(res => (<Response>res).json())
我愿意
.map((res: Response) => res.json())
这篇关于类型“{}"上不存在属性“json"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文