josdejong / svelte-jsoneditor

A web-based tool to view, edit, format, repair, query, transform, and validate JSON
https://jsoneditoronline.org
Other
816 stars 108 forks source link

Change the type of `json` from `JSONValue` to `unknown` #371

Closed josdejong closed 6 months ago

josdejong commented 6 months ago

BREAKING CHANGES IN THE TYPE DEFINITIONS

The JSONValue type is too limited, for example when using the lossless-json parser instead of JSON, there can be instances of LosslessNumber. Other parsers may generated BigInt instances, etc. Therefore, defining json as unknown is more "accurate".

So the following (core) type definitions:

export type TextContent = { text: string } | { json: undefined; text: string }
export type JSONContent = { json: unknown } | { json: unknown; text: undefined }
export type Content = JSONContent | TextContent

Will now become:

export type TextContent = { text: string }
export type JSONContent = { json: unknown }
export type Content = JSONContent | TextContent

Also, the type of JSONParser is changed from type JSONParser = JSON to an explicit description, which has one difference with JSON.stringify namely that it can return string | undefined instead of string. The current official type definition of JSON is lacking in this regard: JSON.stringify(undefined) returns undefined, but this is not reflected in the type, which can lead to a false sense of null-safety. See: https://stackoverflow.com/questions/74461780/is-the-official-type-definition-for-json-stringify-wrong.

All in all, this should result in a more smooth TypeScript experience and less need for type casting.