Using JSON Schema as TypeScript types

. 1 min read
export type JsonSchemaToType<T, R extends string[] = []> = T extends {
	type: 'string';
}
	? string
	: T extends { type: 'number' }
	? number
	: T extends { type: 'boolean' }
	? boolean
	: T extends { type: 'object'; properties: infer P; required?: infer Req }
	? {
			[K in Extract<keyof P, Req[keyof Req]>]: JsonSchemaToType<P[K]>;
	  } & {
			[K in Exclude<keyof P, Req[keyof Req]>]?: JsonSchemaToType<P[K]>;
	  }
	: T extends { type: 'array'; items: infer I }
	? Array<JsonSchemaToType<I>>
	: never;