diff --git a/index.d.ts b/index.d.ts index f478a37..9a1c733 100644 --- a/index.d.ts +++ b/index.d.ts @@ -2,9 +2,10 @@ declare namespace AsTypedInternal { interface SchemaBase { $id?: string; $ref?: string; - type?: string; + type?: string | string[]; title?: string; description?: string; + nullable?: boolean; default?: any; examples?: any[]; } @@ -211,9 +212,11 @@ declare namespace AsTypedInternal { | (ValueType extends StringSchema ? never : string) | (ValueType extends BoolSchema ? never : boolean); - type ResolveRecursiveInternal< - SchemaType - > = SchemaType extends SchemaDeclaration + type ResolveRecursiveInternal = SchemaType extends { + nullable: true; + } + ? ResolveRecursiveInternal> | null + : SchemaType extends SchemaDeclaration ? null : SchemaType extends ConstSchema ? Value @@ -237,6 +240,16 @@ declare namespace AsTypedInternal { > : SchemaType extends ArraySchema ? ResolveArray + : SchemaType extends { + type: [infer Type]; + } + ? ResolveRecursiveInternal & { type: Type }> + : SchemaType extends { + type: [infer Type, ...infer Rest]; + } + ? + | ResolveRecursiveInternal & { type: Type }> + | ResolveRecursiveInternal & { type: Rest }> : never; // TODO diff --git a/test.ts b/test.ts index 2ab84b7..3be1622 100644 --- a/test.ts +++ b/test.ts @@ -11,6 +11,12 @@ assert(_ as AsTyped<{ type: "integer" }>, _ as number); assert(_ as AsTyped<{ type: "string" }>, _ as string); +assert(_ as AsTyped<{ type: ["string", "null"] }>, _ as string | null); + +assert(_ as AsTyped<{ type: "string"; nullable: true }>, _ as string | null); + +assert(_ as AsTyped<{ type: "string"; nullable: false }>, _ as string); + assert(_ as AsTyped<{ type: "boolean" }>, _ as boolean); assert(_ as AsTyped<{ type: "null" }>, _ as null); @@ -182,6 +188,19 @@ assert( _ as { arr?: Array<{ [name: string]: string }> } ); +assert( + _ as AsTyped<{ + type: ["object", "boolean", "null"]; + properties: { + arr: { + type: "array"; + items: { type: "object"; additionalProperties: { type: "string" } }; + }; + }; + }>, + _ as { arr?: Array<{ [name: string]: string }> } | boolean | null +); + /* * assert( * _ as AsTyped<{