属性 'json' 类型“{}”不存在

Property 'json' does not exist on type '{}'

我在 Typescript 中有一个抽象基础 class,如下所示:

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())。我不断收到此错误:

ERROR in ./src/app/components/shared/something/some.abstract.service.ts
(13,29): error TS2339: Property 'json' does not exist on type '{}'.

我遵循了 the angular 2 documentation 中的示例,并且 它有效 。我只是厌倦了盯着这个错误。我错过了什么吗?

您可以通过对 Response:

的类型断言来消除此错误
.map((res: Response) => res.json())

http.post() 将 return 上的 Observable<Response> map 将需要 Response 类型的对象。我认为这是当前 TypeScript 中缺少的定义 AngularJS .d.ts.

对我来说这看起来很奇怪...

.map(res => (<Response>res).json())

我愿意

.map((res: Response) => res.json())