属性“JSON”的类型不存在“{}” [英] Property 'json' does not exist on type '{}'

查看:1637
本文介绍了属性“JSON”的类型不存在“{}”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在打字稿的抽象基类,看起来像这样:

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));
    }
}

它完美。然而,打字稿编译器抱怨 .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 '{}'.

我跟着角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?

推荐答案

您可以摆脱这个错误被类型断言为响应

You can get rid of this error by type-assertion to Response:

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

http.post()将返回观测与LT;响应&GT; 上至极地图将需要类型的对象响应。我认为这是在当前的打字稿AngularJS缺少定义 .d.ts

http.post() will return a Observable<Response> on wich map will require an Object of type Response. I think that's a missing definition in the current TypeScript AngularJS .d.ts.

这篇关于属性“JSON”的类型不存在“{}”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆