2 declare type NumberType = "float32" | "float64" | "int8" | "uint8" | "int16" | "uint16" | "int32" | "uint32";
4 declare type StringType = "string" | "timestamp";
5 /** Generic JTD Schema without inference of the represented type */
6 export declare type SomeJTDSchemaType = (// ref
10 type: NumberType | StringType | "boolean";
14 elements: SomeJTDSchemaType;
16 values: SomeJTDSchemaType;
18 properties: Record<string, SomeJTDSchemaType>;
19 optionalProperties?: Record<string, SomeJTDSchemaType>;
20 additionalProperties?: boolean;
22 properties?: Record<string, SomeJTDSchemaType>;
23 optionalProperties: Record<string, SomeJTDSchemaType>;
24 additionalProperties?: boolean;
26 discriminator: string;
27 mapping: Record<string, SomeJTDSchemaType>;
30 metadata?: Record<string, unknown>;
31 definitions?: Record<string, SomeJTDSchemaType>;
33 /** required keys of an object, not undefined */
34 declare type RequiredKeys<T> = {
35 [K in keyof T]-?: undefined extends T[K] ? never : K;
37 /** optional or undifined-able keys of an object */
38 declare type OptionalKeys<T> = {
39 [K in keyof T]-?: undefined extends T[K] ? K : never;
41 /** type is true if T is a union type */
42 declare type IsUnion_<T, U extends T = T> = false extends (T extends unknown ? ([U] extends [T] ? false : true) : never) ? false : true;
43 declare type IsUnion<T> = IsUnion_<T>;
44 /** type is true if T is identically E */
45 declare 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*/
47 declare 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 */
49 declare 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 */
51 declare type IsEnum<T> = null extends EnumString<Exclude<T, null>> ? false : true;
52 /** true only if all types are array types (not tuples) */
53 declare 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 */
55 declare 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 */
57 declare type IsRecord<T, Union extends boolean> = Union extends IsUnion<Exclude<T, null>> ? null extends EnumString<keyof Exclude<T, null>> ? false : true : false;
59 export 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]] ? {
66 }) | (null extends T ? {
67 [K in keyof D]: [Exclude<T, null>] extends [Exclude<D[K], null>] ? {
72 } : never)) | (unknown extends T ? {
74 } : never) | ((true extends NullTypeEquality<T, number> ? {
76 } : true extends NullTypeEquality<T, boolean> ? {
78 } : true extends NullTypeEquality<T, string> ? {
80 } : true extends NullTypeEquality<T, Date> ? {
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>;
92 [K in RequiredKeys<T>]: JTDSchemaType<T[K], D>;
94 }) & ([OptionalKeys<Exclude<T, null>>] extends [never] ? {
95 optionalProperties?: Record<string, never>;
98 [K in OptionalKeys<T>]: JTDSchemaType<Exclude<T[K], undefined>, D>;
101 additionalProperties?: boolean;
102 } : true extends IsRecord<T, true> ? {
103 [K in keyof Exclude<T, null>]-?: Exclude<T, null>[K] extends string ? {
106 [M in Exclude<T, null>[K]]: JTDSchemaType<Omit<T extends {
108 } ? T : never, K>, D>;
111 }[keyof Exclude<T, null>] : never) & (null extends T ? {
116 metadata?: Record<string, unknown>;
118 [K in keyof D]: JTDSchemaType<D[K], D>;
121 declare type JTDDataDef<S, D extends Record<string, unknown>> = (S extends {
124 [K in S["ref"]]: infer V;
125 } ? JTDDataDef<V, D> : never : S extends {
127 } ? number : S extends {
129 } ? boolean : S extends {
131 } ? string : S extends {
133 } ? string | Date : S extends {
134 enum: readonly (infer E)[];
135 } ? string extends E ? never : [E] extends [string] ? E : never : S extends {
137 } ? JTDDataDef<E, D>[] : S extends {
138 properties: Record<string, unknown>;
139 optionalProperties?: Record<string, unknown>;
140 additionalProperties?: boolean;
142 -readonly [K in keyof S["properties"]]-?: JTDDataDef<S["properties"][K], D>;
144 -readonly [K in keyof S["optionalProperties"]]+?: JTDDataDef<S["optionalProperties"][K], D>;
145 } & ([S["additionalProperties"]] extends [true] ? Record<string, unknown> : unknown) : S extends {
146 properties?: Record<string, unknown>;
147 optionalProperties: Record<string, unknown>;
148 additionalProperties?: boolean;
150 -readonly [K in keyof S["properties"]]-?: JTDDataDef<S["properties"][K], D>;
152 -readonly [K in keyof S["optionalProperties"]]+?: JTDDataDef<S["optionalProperties"][K], D>;
153 } & ([S["additionalProperties"]] extends [true] ? Record<string, unknown> : unknown) : S extends {
155 } ? Record<string, JTDDataDef<V, D>> : S extends {
156 discriminator: infer M;
157 mapping: Record<string, unknown>;
158 } ? [M] extends [string] ? {
159 [K in keyof S["mapping"]]: JTDDataDef<S["mapping"][K], D> & {
162 }[keyof S["mapping"]] : never : unknown) | (S extends {
165 export declare type JTDDataType<S> = S extends {
166 definitions: Record<string, unknown>;
167 } ? JTDDataDef<S, S["definitions"]> : JTDDataDef<S, Record<string, never>>;