2019-02-24 02:45:27 +02:00
|
|
|
export type Schema = {
|
2019-04-23 16:35:26 +03:00
|
|
|
type: 'boolean' | 'number' | 'string' | 'array' | 'object' | 'any';
|
|
|
|
nullable: boolean;
|
|
|
|
optional: boolean;
|
2019-02-24 02:45:27 +02:00
|
|
|
items?: Schema;
|
|
|
|
properties?: Obj;
|
|
|
|
description?: string;
|
2019-04-23 16:35:26 +03:00
|
|
|
example?: any;
|
|
|
|
format?: string;
|
|
|
|
ref?: string;
|
|
|
|
enum?: string[];
|
2019-02-24 02:45:27 +02:00
|
|
|
};
|
|
|
|
|
2019-04-23 16:35:26 +03:00
|
|
|
type NonUndefinedPropertyNames<T extends Obj> = {
|
|
|
|
[K in keyof T]: T[K]['optional'] extends true ? never : K
|
|
|
|
}[keyof T];
|
|
|
|
|
|
|
|
type UndefinedPropertyNames<T extends Obj> = {
|
|
|
|
[K in keyof T]: T[K]['optional'] extends true ? K : never
|
|
|
|
}[keyof T];
|
|
|
|
|
|
|
|
type OnlyRequired<T extends Obj> = Pick<T, NonUndefinedPropertyNames<T>>;
|
|
|
|
type OnlyOptional<T extends Obj> = Pick<T, UndefinedPropertyNames<T>>;
|
|
|
|
|
2019-02-24 02:45:27 +02:00
|
|
|
export type Obj = { [key: string]: Schema };
|
|
|
|
|
2019-04-23 16:35:26 +03:00
|
|
|
export type ObjType<s extends Obj> =
|
|
|
|
{ [P in keyof OnlyOptional<s>]?: SchemaType<s[P]> } &
|
|
|
|
{ [P in keyof OnlyRequired<s>]: SchemaType<s[P]> };
|
2019-02-24 02:45:27 +02:00
|
|
|
|
|
|
|
// https://qiita.com/hrsh7th@github/items/84e8968c3601009cdcf2
|
|
|
|
type MyType<T extends Schema> = {
|
|
|
|
0: any;
|
|
|
|
1: SchemaType<T>;
|
|
|
|
}[T extends Schema ? 1 : 0];
|
|
|
|
|
2019-04-23 16:35:26 +03:00
|
|
|
type NullOrUndefined<p extends Schema, T> =
|
|
|
|
p['nullable'] extends true
|
|
|
|
? p['optional'] extends true
|
|
|
|
? (T | null | undefined)
|
|
|
|
: (T | null)
|
|
|
|
: p['optional'] extends true
|
|
|
|
? (T | undefined)
|
|
|
|
: T;
|
|
|
|
|
2019-02-24 02:45:27 +02:00
|
|
|
export type SchemaType<p extends Schema> =
|
2019-04-23 16:35:26 +03:00
|
|
|
p['type'] extends 'number' ? NullOrUndefined<p, number> :
|
|
|
|
p['type'] extends 'string' ? NullOrUndefined<p, string> :
|
|
|
|
p['type'] extends 'boolean' ? NullOrUndefined<p, boolean> :
|
|
|
|
p['type'] extends 'array' ? NullOrUndefined<p, MyType<NonNullable<p['items']>>[]> :
|
|
|
|
p['type'] extends 'object' ? NullOrUndefined<p, ObjType<NonNullable<p['properties']>>> :
|
|
|
|
p['type'] extends 'any' ? NullOrUndefined<p, any> :
|
2019-02-24 02:45:27 +02:00
|
|
|
any;
|