Last active
May 2, 2024 18:19
-
-
Save nathansmith/fedc7554944e53422511e5c788a0226c to your computer and use it in GitHub Desktop.
Example of JSON in TypeScript.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export type IJson = IJsonArray | IJsonObject | boolean | number | null | string; | |
export type IJsonArray = IJson[]; | |
/* | |
===== | |
NOTE: | |
===== | |
This is an `interface` because the reference | |
can be circular. It would change to `any` if | |
we simply used a `type`. This does not work… | |
``` | |
export type IJsonObject = Record<string, IJson>; | |
``` | |
*/ | |
export interface IJsonObject { | |
[key: string]: IJson; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
===== | |
NOTE: | |
===== | |
Alternatively, a more generic `object` approach may be simpler. | |
Be aware, this includes both `Array` and `Object`. | |
*/ | |
export type IJson = boolean | null | number | object | string; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment