tatethurston / TwirpScript

A protobuf RPC framework for JavaScript and TypeScript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Are there any plans to natively support `int64`?

antonioorct opened this issue · comments

I think right now proto's int64 in js has the type of string. Maybe if you could support it by using js' BigInt type or something similar?

Totally. I punted on making a decision here until someone had a use case. A number of libraries use https://github.com/dcodeIO/Long.js/, but I think that is because they predate general support for BigInt.

BigInt looks well supported https://caniuse.com/bigint https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt so I'll implement it. Down the road if someone prefers to use strings I can enable type overrides pretty easily, but BigInt seems like the correct default.

One call out: BigInt doesn't JSON serialize natively. This will require add custom JSON serialization to TwirpScript, which is a breaking change. Running yarn twirpscript to regenerate pb.ts files should be the only action needed to resolve.

On a related note, @antonioorct what are your thoughts on using Map instead of an object literal as the generated type for maps? This would be a breaking change because Map has a different interface than a plain JavaScript object.

The motivation is that keys in JS object literals are strings, where Map preserves the type -- this is relevant in the case where you use a bigint as the key.

@tatethurston well, that seems like a logical assessment, although i personally wouldn't need to have this implemented, as for now i haven't had a use case where i had to use anything other than string or number as an index of a map object, not only on my current project but any other project.