首页 > 解决方案 > 角http响应中的大整数(长)被四舍五入

问题描述

我正在调用返回的 Web 服务

[
    {
        "product_id": 4253579243953800463,
        "timestamp": 1525107472,
        "event_type": "PRODUCT_VIEW",
        "event_count": 1
    },
    {
        "product_id": 788410946016999754,
        "timestamp": 1525107472,
        "event_type": "PRODUCT_VIEW",
        "event_count": 1
    }
]

我在我的 Angular 代码中编写了一个服务,该服务调用了这个后端服务

  getLastRecommendations(): Observable<UserProductEvent[]> {
   return this.http.get<UserProductEvent[]>(url, HTTP_OPTIONS)
  }

我看到产品ID的响应是long类型,并且它的值大于javascript中的Number.MAX_SAFE_INTEGER,所以我想强制将产品ID视为字符串。否则将值四舍五入为

[
    {
        "product_id": 4253579243953800700,
        "timestamp": 1525107472,
        "event_type": "PRODUCT_VIEW",
        "event_count": 1
    },
    {
        "product_id": 788410946016999800,
        "timestamp": 1525107472,
        "event_type": "PRODUCT_VIEW",
        "event_count": 1
    }
]

我尝试在我的打字稿接口声明中将 product_id 的类型更改为字符串,但它仍然不起作用。

如何强制 typescript/javascript 将该字段视为字符串?

有人可以帮忙吗?

export interface UserProductEvent {
    product_id: string;
    timestamp: number;
    event_type: string;
    event_count: number;
}

而且我无法更改后端。:(

标签: angulartypescripthttpcasting

解决方案


推荐阅读