Skip to content

Instantly share code, notes, and snippets.

@supermacro
Created November 1, 2018 20:29
Show Gist options
  • Save supermacro/20685160adac9208f0ed45720788461b to your computer and use it in GitHub Desktop.
Save supermacro/20685160adac9208f0ed45720788461b to your computer and use it in GitHub Desktop.
types for defining JSON schemas
type Required<T> = {
[K in keyof T]: Schema<T>
}
interface ObjectSchema<T> {
type: 'object'
required: Array<keyof T>
properties: Required<T>
}
interface ArraySchema<T> {
type: 'array'
items: ObjectSchema<T>
}
type Primitive
= 'string'
| 'number'
| 'boolean'
| 'null'
type PrimitiveSchema
= { type: Primitive }
| { type: Array<Primitive>}
type Schema<T>
= ObjectSchema<T>
| ArraySchema<T>
| PrimitiveSchema
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment