Created
July 19, 2020 12:44
-
-
Save karol-majewski/98794005860f926e573db3cd33ed889e to your computer and use it in GitHub Desktop.
JSONLike type in TypeScript
This file contains hidden or 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
type JSONLike = | |
| { [property: string]: JSONLike } | |
| readonly JSONLike[] | |
| string | |
| number | |
| boolean | |
| null; | |
interface Serializable { | |
toJSON(): JSONLike; | |
} | |
class MyClass implements Serializable { | |
map = new Map<string, string>(); | |
toJSON() { | |
// return this.map; Error | |
return Object.fromEntries(this.map.entries()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment