Representing arbitrary JSON values. #337
Replies: 1 comment 3 replies
-
@maxgurewitz Hi, I would recommend using export type JSONValue = null | string | number | boolean | Record<string, JSONValue> | Array<JSONValue>
// ^ Type alias 'JSONValue' circularly references itself. In practice though, both the function receive(value: Static<typeof JSONValue>) {
// note: explicit narrowing of value is required before use
if(typeof value === 'string') console.log(value.toLowerCase())
if(typeof value === 'number') console.log(value.toFixed(2))
// and so on
} But this is true for const JSONValue = Type.Unknown()
function receive(value: Static<typeof JSONValue>) {
// note: explicit narrowing of value is required before use
if(typeof value === 'string') console.log(value.toLowerCase())
if(typeof value === 'number') console.log(value.toFixed(2))
// and so on
} So, from this, using |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello, I'm trying to represent the following type,
Which is intended to represent an arbitrary JSON value.
However, typebox seems to be missing support for index signatures for
{ [x: string]: JSONValue }
. I can't useRecord
's instead,because I get the error
Type instantiation is excessively deep and possibly infinite
.Any tips? Thank you!
Beta Was this translation helpful? Give feedback.
All reactions