UNPKG

6.91 kBTypeScriptView Raw
1/** numeric strings */
2declare type NumberType = "float32" | "float64" | "int8" | "uint8" | "int16" | "uint16" | "int32" | "uint32";
3/** string strings */
4declare type StringType = "string" | "timestamp";
5/** Generic JTD Schema without inference of the represented type */
6export declare type SomeJTDSchemaType = (// ref
7{
8 ref: string;
9} | {
10 type: NumberType | StringType | "boolean";
11} | {
12 enum: string[];
13} | {
14 elements: SomeJTDSchemaType;
15} | {
16 values: SomeJTDSchemaType;
17} | {
18 properties: Record<string, SomeJTDSchemaType>;
19 optionalProperties?: Record<string, SomeJTDSchemaType>;
20 additionalProperties?: boolean;
21} | {
22 properties?: Record<string, SomeJTDSchemaType>;
23 optionalProperties: Record<string, SomeJTDSchemaType>;
24 additionalProperties?: boolean;
25} | {
26 discriminator: string;
27 mapping: Record<string, SomeJTDSchemaType>;
28} | {}) & {
29 nullable?: boolean;
30 metadata?: Record<string, unknown>;
31 definitions?: Record<string, SomeJTDSchemaType>;
32};
33/** required keys of an object, not undefined */
34declare type RequiredKeys<T> = {
35 [K in keyof T]-?: undefined extends T[K] ? never : K;
36}[keyof T];
37/** optional or undifined-able keys of an object */
38declare type OptionalKeys<T> = {
39 [K in keyof T]-?: undefined extends T[K] ? K : never;
40}[keyof T];
41/** type is true if T is a union type */
42declare type IsUnion_<T, U extends T = T> = false extends (T extends unknown ? ([U] extends [T] ? false : true) : never) ? false : true;
43declare type IsUnion<T> = IsUnion_<T>;
44/** type is true if T is identically E */
45declare type TypeEquality<T, E> = [T] extends [E] ? ([E] extends [T] ? true : false) : false;
46/** type is true if T or null is identically E or null*/
47declare type NullTypeEquality<T, E> = TypeEquality<T | null, E | null>;
48/** gets only the string literals of a type or null if a type isn't a string literal */
49declare type EnumString<T> = [T] extends [never] ? null : T extends string ? string extends T ? null : T : null;
50/** true if type is a union of string literals */
51declare type IsEnum<T> = null extends EnumString<Exclude<T, null>> ? false : true;
52/** true only if all types are array types (not tuples) */
53declare type IsElements<T> = false extends IsUnion<T> ? [T] extends [readonly unknown[]] ? undefined extends T[0.5] ? false : true : false : false;
54/** true if the the type is a values type */
55declare type IsValues<T> = false extends IsUnion<Exclude<T, null>> ? TypeEquality<keyof Exclude<T, null>, string> : false;
56/** true if type is a proeprties type and Union is false, or type is a discriminator type and Union is true */
57declare type IsRecord<T, Union extends boolean> = Union extends IsUnion<Exclude<T, null>> ? null extends EnumString<keyof Exclude<T, null>> ? false : true : false;
58/** actual schema */
59export declare type JTDSchemaType<T, D extends Record<string, unknown> = Record<string, never>> = (// refs - where null wasn't specified, must match exactly
60(null extends EnumString<keyof D> ? never : ({
61 [K in keyof D]: [T] extends [D[K]] ? {
62 ref: K;
63 } : never;
64}[keyof D] & {
65 nullable?: false;
66}) | (null extends T ? {
67 [K in keyof D]: [Exclude<T, null>] extends [Exclude<D[K], null>] ? {
68 ref: K;
69 } : never;
70}[keyof D] & {
71 nullable: true;
72} : never)) | (unknown extends T ? {
73 nullable?: boolean;
74} : never) | ((true extends NullTypeEquality<T, number> ? {
75 type: NumberType;
76} : true extends NullTypeEquality<T, boolean> ? {
77 type: "boolean";
78} : true extends NullTypeEquality<T, string> ? {
79 type: StringType;
80} : true extends NullTypeEquality<T, Date> ? {
81 type: "timestamp";
82} : true extends IsEnum<T> ? {
83 enum: EnumString<Exclude<T, null>>[];
84} : true extends IsElements<Exclude<T, null>> ? T extends readonly (infer E)[] ? {
85 elements: JTDSchemaType<E, D>;
86} : never : true extends IsValues<T> ? T extends Record<string, infer V> ? {
87 values: JTDSchemaType<V, D>;
88} : never : true extends IsRecord<T, false> ? ([RequiredKeys<Exclude<T, null>>] extends [never] ? {
89 properties?: Record<string, never>;
90} : {
91 properties: {
92 [K in RequiredKeys<T>]: JTDSchemaType<T[K], D>;
93 };
94}) & ([OptionalKeys<Exclude<T, null>>] extends [never] ? {
95 optionalProperties?: Record<string, never>;
96} : {
97 optionalProperties: {
98 [K in OptionalKeys<T>]: JTDSchemaType<Exclude<T[K], undefined>, D>;
99 };
100}) & {
101 additionalProperties?: boolean;
102} : true extends IsRecord<T, true> ? {
103 [K in keyof Exclude<T, null>]-?: Exclude<T, null>[K] extends string ? {
104 discriminator: K;
105 mapping: {
106 [M in Exclude<T, null>[K]]: JTDSchemaType<Omit<T extends {
107 [C in K]: M;
108 } ? T : never, K>, D>;
109 };
110 } : never;
111}[keyof Exclude<T, null>] : never) & (null extends T ? {
112 nullable: true;
113} : {
114 nullable?: false;
115}))) & {
116 metadata?: Record<string, unknown>;
117 definitions?: {
118 [K in keyof D]: JTDSchemaType<D[K], D>;
119 };
120};
121declare type JTDDataDef<S, D extends Record<string, unknown>> = // ref
122(S extends {
123 ref: string;
124} ? D extends {
125 [K in S["ref"]]: infer V;
126} ? JTDDataDef<V, D> : never : S extends {
127 type: NumberType;
128} ? number : S extends {
129 type: "boolean";
130} ? boolean : S extends {
131 type: "string";
132} ? string : S extends {
133 type: "timestamp";
134} ? string | Date : S extends {
135 enum: readonly (infer E)[];
136} ? string extends E ? never : [E] extends [string] ? E : never : S extends {
137 elements: infer E;
138} ? JTDDataDef<E, D>[] : S extends {
139 properties: Record<string, unknown>;
140 optionalProperties?: Record<string, unknown>;
141 additionalProperties?: boolean;
142} ? {
143 -readonly [K in keyof S["properties"]]-?: JTDDataDef<S["properties"][K], D>;
144} & {
145 -readonly [K in keyof S["optionalProperties"]]+?: JTDDataDef<S["optionalProperties"][K], D>;
146} & ([S["additionalProperties"]] extends [true] ? Record<string, unknown> : unknown) : S extends {
147 properties?: Record<string, unknown>;
148 optionalProperties: Record<string, unknown>;
149 additionalProperties?: boolean;
150} ? {
151 -readonly [K in keyof S["properties"]]-?: JTDDataDef<S["properties"][K], D>;
152} & {
153 -readonly [K in keyof S["optionalProperties"]]+?: JTDDataDef<S["optionalProperties"][K], D>;
154} & ([S["additionalProperties"]] extends [true] ? Record<string, unknown> : unknown) : S extends {
155 values: infer V;
156} ? Record<string, JTDDataDef<V, D>> : S extends {
157 discriminator: infer M;
158 mapping: Record<string, unknown>;
159} ? [M] extends [string] ? {
160 [K in keyof S["mapping"]]: JTDDataDef<S["mapping"][K], D> & {
161 [KM in M]: K;
162 };
163}[keyof S["mapping"]] : never : unknown) | (S extends {
164 nullable: true;
165} ? null : never);
166export declare type JTDDataType<S> = S extends {
167 definitions: Record<string, unknown>;
168} ? JTDDataDef<S, S["definitions"]> : JTDDataDef<S, Record<string, never>>;
169export {};