为什么在angular 4.3中的httpclient返回Object而不是任何对象? [英] Why does the httpclient in angular 4.3 return Object instead of any?

查看:122
本文介绍了为什么在angular 4.3中的httpclient返回Object而不是任何对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,Angular 4.3中新的HttpClient类似乎返回 Object 而不是 any .

鉴于打字稿文档说:

永远不要使用数字,字符串,布尔值或对象类型.这些 类型是指几乎从未使用过的非原始盒装对象 适当地用JavaScript代码编写.

https://www. typescriptlang.org/docs/handbook/declaration-files/do-s-and-don-ts.html

我知道我可以使用以下方式提供自己的返回类型:
this.httpService.get<any>('/api1').subscribe(Data => {console.log(Data.Value1)});

将其设置为默认值似乎更容易.我知道我可以为返回的数据创建特定的类型,但是使用任何似乎都可以使其更加灵活.

我打算扩展HttpClient并重写方法以返回任何值,但是在我这样做之前,我想查看是否缺少某些内容.

解决方案

几乎没有在JavaScript代码中正确使用的非原始装箱对象中的关键字是 almost .

此答案中所述,JSON文档可以是除undefined和符号之外的任何内容,这使Objectany更合适的类型.

中解释了anyObject的行为.手册:

任何类型都是使用现有JavaScript的强大方法,可让您在编译过程中逐渐选择加入和选择退出类型检查.您可能希望Object像其他语言一样扮演相似的角色.但是Object类型的变量仅允许您为它们分配任何值-您无法在它们上调用任意方法,即使是实际存在的方法

any响应类型允许在其上调用任意方法,这是不理想的行为,包括不存在的行为.

适合JSON响应的类型是

any[] | object | boolean | number | string | null

精确的一个(如此处所示, null)的附加内容是:

type JSONValue = boolean | number | string | null | JSONObject | JSONArray;

interface JSONObject {
    [x: string]: JSONValue;
}

interface JSONArray extends Array<JSONValue> { }

这可能是应该在HttpClient中完成的方式.与any相比,Object的准确性较差,但适用性更高.

The new HttpClient class in Angular 4.3 seems to return Object instead of any by default.

Is there a particular reason for doing that, given the typescript documentation says:

Don’t ever use the types Number, String, Boolean, or Object. These types refer to non-primitive boxed objects that are almost never used appropriately in JavaScript code.

https://www.typescriptlang.org/docs/handbook/declaration-files/do-s-and-don-ts.html

I'm aware that I can provide my own return type using:
this.httpService.get<any>('/api1').subscribe(Data => {console.log(Data.Value1)});

It would seem to be easier to just make it the default. I'm aware that I can create a type specific for the data it's returning, but using any seems like it'd make it more flexible.

I was going to extend the HttpClient and override the methods to return any, but before I do that I wanted to see if there was something I was missing.

解决方案

The keyword in non-primitive boxed objects that are almost never used appropriately in JavaScript code is almost.

As explained in this answer, JSON document can be anything but undefined and symbol, this makes Object more suitable type than any.

The behaviour of any versus Object is explained in the manual:

The any type is a powerful way to work with existing JavaScript, allowing you to gradually opt-in and opt-out of type-checking during compilation. You might expect Object to play a similar role, as it does in other languages. But variables of type Object only allow you to assign any value to them - you can’t call arbitrary methods on them, even ones that actually exist

any response type allows to call arbitrary methods on it, which is not a desirable behaviour, including the ones that don't exist.

A suitable type for JSON response is

any[] | object | boolean | number | string | null

And a precise one (like is shown here, with the addition of null) is:

type JSONValue = boolean | number | string | null | JSONObject | JSONArray;

interface JSONObject {
    [x: string]: JSONValue;
}

interface JSONArray extends Array<JSONValue> { }

This is likely the way it should be done in HttpClient. While Object is less accurate yet still more applicable type than any.

这篇关于为什么在angular 4.3中的httpclient返回Object而不是任何对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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