Skip to content

Commit

Permalink
feat(types): add JSONArray, update JSONObject and JSONValuedefinitions
Browse files Browse the repository at this point in the history
  • Loading branch information
unicornware committed May 24, 2021
1 parent 8b690b0 commit a609635
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,31 @@ export type EmptyPrimitive = '' | null | undefined
*/
export type IndexSignature = number | symbol | string

/**
* Types representing an array of [JSON data types][1].
*
* [1]: https://restfulapi.net/json-data-types
*/
export type JSONArray = JSONValue[]

/**
* Type representing any [JSON][1] object.
*
* This type can be useful to enforce some input to be JSON-compatible or as a
* super-type to be extended from.
*
* Don't use this as a direct return type as the user would have to double-cast
* it: `object as unknown as CustomResponse`.
*
* Instead, you could extend `JSONObject` to ensure your type only uses
* JSON-compatible types: `interface CustomResponse extends JSONObject { … }`.
*
* Reference: [type-fest - Basic][2]
*
* [1]: https://restfulapi.net/json-data-types
* [2]: https://github.com/sindresorhus/type-fest/blob/main/source/basic.d.ts
*/
export type JSONObject = Record<string, JSONValue>
export type JSONObject = { [Key in string]?: JSONValue }

/**
* Type representing any [primitive][1] [JSON value][2].
Expand All @@ -56,12 +75,9 @@ export type JSONPrimitive = boolean | null | number | string
/**
* Types of [JSON data types][1].
*
* `PlainObject` must be used to represent `JSONObject` to prevent the circular
* referencing error: `"Type alias 'JSONValue' circularly references itself"`.
*
* [1]: https://restfulapi.net/json-data-types
*/
export type JSONValue = OneOrMany<JSONPrimitive | PlainObject>
export type JSONValue = JSONArray | JSONPrimitive | JSONObject

/**
* Type representing any boolean that can also be `null`.
Expand Down

0 comments on commit a609635

Please sign in to comment.