1 /** required keys of an object, not undefined */
2 declare type RequiredKeys<T> = {
3 [K in keyof T]-?: undefined extends T[K] ? never : K;
5 /** optional or undifined-able keys of an object */
6 declare type OptionalKeys<T> = {
7 [K in keyof T]-?: undefined extends T[K] ? K : never;
9 /** type is true if T is a union type */
10 declare type IsUnion_<T, U extends T = T> = false extends (T extends unknown ? ([U] extends [T] ? false : true) : never) ? false : true;
11 declare type IsUnion<T> = IsUnion_<T>;
12 /** type is true if T is identically E */
13 declare type TypeEquality<T, E> = [T] extends [E] ? ([E] extends [T] ? true : false) : false;
14 /** type is true if T or null is identically E or null*/
15 declare type NullTypeEquality<T, E> = TypeEquality<T | null, E | null>;
16 /** gets only the string literals of a type or null if a type isn't a string literal */
17 declare type EnumString<T> = [T] extends [never] ? null : T extends string ? string extends T ? null : T : null;
18 /** true if type is a union of string literals */
19 declare type IsEnum<T> = null extends EnumString<Exclude<T, null>> ? false : true;
20 /** true only if all types are array types (not tuples) */
21 declare type IsElements<T> = false extends IsUnion<T> ? [T] extends [readonly unknown[]] ? undefined extends T[0.5] ? false : true : false : false;
22 /** true if the the type is a values type */
23 declare type IsValues<T> = false extends IsUnion<Exclude<T, null>> ? TypeEquality<keyof Exclude<T, null>, string> : false;
24 /** true if type is a proeprties type and Union is false, or type is a discriminator type and Union is true */
25 declare type IsRecord<T, Union extends boolean> = Union extends IsUnion<Exclude<T, null>> ? null extends EnumString<keyof Exclude<T, null>> ? false : true : false;
26 /** numeric strings */
27 declare type NumberType = "float32" | "float64" | "int8" | "uint8" | "int16" | "uint16" | "int32" | "uint32";
29 declare type StringType = "string" | "timestamp";
31 export declare type JTDSchemaType<T, D extends Record<string, unknown> = Record<string, never>> = (// refs - where null wasn't specified, must match exactly
32 (null extends EnumString<keyof D> ? never : ({
33 [K in keyof D]: [T] extends [D[K]] ? {
38 }) | (null extends T ? {
39 [K in keyof D]: [Exclude<T, null>] extends [Exclude<D[K], null>] ? {
44 } : never)) | (unknown extends T ? {
46 } : never) | ((true extends NullTypeEquality<T, number> ? {
48 } : true extends NullTypeEquality<T, boolean> ? {
50 } : true extends NullTypeEquality<T, string> ? {
52 } : true extends NullTypeEquality<T, Date> ? {
54 } : true extends IsEnum<T> ? {
55 enum: EnumString<Exclude<T, null>>[];
56 } : true extends IsElements<Exclude<T, null>> ? T extends readonly (infer E)[] ? {
57 elements: JTDSchemaType<E, D>;
58 } : never : true extends IsValues<T> ? T extends Record<string, infer V> ? {
59 values: JTDSchemaType<V>;
60 } : never : true extends IsRecord<T, false> ? ([RequiredKeys<Exclude<T, null>>] extends [never] ? {
61 properties?: Record<string, never>;
64 [K in RequiredKeys<T>]: JTDSchemaType<T[K], D>;
66 }) & ([OptionalKeys<Exclude<T, null>>] extends [never] ? {
67 optionalProperties?: Record<string, never>;
70 [K in OptionalKeys<T>]: JTDSchemaType<Exclude<T[K], undefined>, D>;
73 additionalProperties?: boolean;
74 } : true extends IsRecord<T, true> ? {
75 [K in keyof Exclude<T, null>]-?: Exclude<T, null>[K] extends string ? {
78 [M in Exclude<T, null>[K]]: JTDSchemaType<Omit<T extends {
80 } ? T : never, K>, D>;
83 }[keyof Exclude<T, null>] : never) & (null extends T ? {
88 metadata?: Record<string, unknown>;
90 [K in keyof D]: JTDSchemaType<D[K], D>;
93 declare type JTDDataDef<S, D extends Record<string, unknown>> = (S extends {
95 } ? JTDDataDef<D[S["ref"]], D> : S extends {
97 } ? number : S extends {
99 } ? string : S extends {
101 } ? string | Date : S extends {
102 enum: readonly (infer E)[];
103 } ? string extends E ? never : [E] extends [string] ? E : never : S extends {
105 } ? JTDDataDef<E, D>[] : S extends {
106 properties: Record<string, unknown>;
107 optionalProperties?: Record<string, unknown>;
108 additionalProperties?: boolean;
110 -readonly [K in keyof S["properties"]]-?: JTDDataDef<S["properties"][K], D>;
112 -readonly [K in keyof S["optionalProperties"]]+?: JTDDataDef<S["optionalProperties"][K], D>;
113 } & ([S["additionalProperties"]] extends [true] ? Record<string, unknown> : unknown) : S extends {
114 properties?: Record<string, unknown>;
115 optionalProperties: Record<string, unknown>;
116 additionalProperties?: boolean;
118 -readonly [K in keyof S["properties"]]-?: JTDDataDef<S["properties"][K], D>;
120 -readonly [K in keyof S["optionalProperties"]]+?: JTDDataDef<S["optionalProperties"][K], D>;
121 } & ([S["additionalProperties"]] extends [true] ? Record<string, unknown> : unknown) : S extends {
123 } ? Record<string, JTDDataDef<V, D>> : S extends {
124 discriminator: infer M;
125 mapping: Record<string, unknown>;
126 } ? [M] extends [string] ? {
127 [K in keyof S["mapping"]]: JTDDataDef<S["mapping"][K], D> & {
130 }[keyof S["mapping"]] : never : unknown) | (S extends {
133 export declare type JTDDataType<S> = S extends {
134 definitions: Record<string, unknown>;
135 } ? JTDDataDef<S, S["definitions"]> : JTDDataDef<S, Record<string, never>>;