You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When performing a JSON.parse from the above JSON into the above interface or its equivalent type, the example64 number would be 9007199254740992 (ending in 2) despite the JSON value being 9007199254740993 (ending in 3).
I'm working on a system which sends large numbers between Go and TypeScript components via JSON.
The Go code which can 'marshal' a struct containing an int64 to JSON would produce the number as expected in JSON.
{
"example64":9007199254740993
}
This is because JSON and JSON Schema does not define precision of numbers (see Context for more on this).
Here's an example of some Go code (note the maximum signed integer value for 32-bit integers in Go is 2147483647):
Currently the generated TypeScript code infers the number type, even when the number 9007199254740993 or larger would not be able to be parsed into then stringify'd back into the JSON file by the generated TypeScript type.
Proposed Behaviour / Output
Quicktype should detect the size of the number and use the bigint type instead.
Context
As outlined here, "The JSON Schema data model explicitly includes arbitrary-precision numbers."
The latest JSON Schema (2020-12) document here defines number: An arbitrary-precision, base-10 decimal number value, from the JSON "number" value.
Currently 64-bit integers use the
number
type in generated TypeScript files, but should bebigint
type instead.Context (Input, Language)
Example JSON input:
Example TypeScript output:
This conversion can easily be verified at https://app.quicktype.io/?l=ts
When performing a JSON.parse from the above JSON into the above interface or its equivalent type, the example64 number would be
9007199254740992
(ending in 2) despite the JSON value being9007199254740993
(ending in 3).The correct TypeScript output should be:
Description
I'm working on a system which sends large numbers between Go and TypeScript components via JSON.
The Go code which can 'marshal' a struct containing an int64 to JSON would produce the number as expected in JSON.
This is because JSON and JSON Schema does not define precision of numbers (see Context for more on this).
Here's an example of some Go code (note the maximum signed integer value for 32-bit integers in Go is
2147483647
):You can run the above Go code here: https://go.dev/play/p/yHYyqAxzUr3
Current Behaviour / Output
Currently the generated TypeScript code infers the
number
type, even when the number 9007199254740993 or larger would not be able to be parsed into then stringify'd back into the JSON file by the generated TypeScript type.Proposed Behaviour / Output
Quicktype should detect the size of the number and use the
bigint
type instead.Context
As outlined here, "The JSON Schema data model explicitly includes arbitrary-precision numbers."
The latest JSON Schema (2020-12) document here defines
number: An arbitrary-precision, base-10 decimal number value, from the JSON "number" value
.MDN BigInt reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt
For more context on how BigInt works in TypeScript see this TypeScript playground example (note it must be ES2020 or later) for the code below:
The text was updated successfully, but these errors were encountered: