微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。