gRPC-web 打字稿如何从长转换为数字

问题描述

我正在使用 gRPC-web,我从服务器获得了 id 类型为 number|Long 的数据。 我需要将 number|Long 分配给 numberLong 来自使用 gRPC 进行通信的服务器。
错误

Type 'number | Long' is not assignable to type 'number'.   Type 'Long' is not assignable to type 'number'. 

当我尝试 parseInt() 时,它说:

Type 'number' is not assignable to type 'string'

解决方法

Long to number 是一种有损操作,因此您无法在没有任何精度损失的情况下安全地执行此操作。但是您可以使用 .toNumber() 方法。

https://github.com/dcodeIO/long.js