From 0d67f1d0cfab1f404d9676c943569bb5d8294f4d Mon Sep 17 00:00:00 2001 From: Guilherme Bernal Date: Wed, 7 Sep 2022 14:42:51 +0000 Subject: [PATCH] feat: allow objects to be specified without "type": "object" --- index.d.ts | 34 +++++++++++++++++----------------- test.ts | 8 ++++++++ 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/index.d.ts b/index.d.ts index b52bf46..8b68b15 100644 --- a/index.d.ts +++ b/index.d.ts @@ -97,23 +97,6 @@ declare namespace AsTypedInternal { : AsTypedTupleSchemaWithAdditional : SchemaType extends { not: infer T } ? ResolveNot - : SchemaType extends { - type: "object"; - required?: infer Required; - properties?: infer Props; - additionalProperties?: infer AdditionalProperties; - } - ? ResolveObject< - Props, - Required extends undefined - ? unknown - : Required extends string[] - ? string extends Required[number] - ? unknown - : Required[number] - : unknown, - AdditionalProperties - > : SchemaType extends { type: "array"; items: infer ValueType } ? Array> : SchemaType extends { @@ -134,6 +117,23 @@ declare namespace AsTypedInternal { ? ResolveAllOf : SchemaType extends { if: infer If; then: infer Then; else?: infer Else } ? (Resolve & Resolve) | Resolve + : SchemaType extends { + type?: "object"; + required?: infer Required; + properties?: infer Props; + additionalProperties?: infer AdditionalProperties; + } + ? ResolveObject< + Props, + Required extends undefined + ? unknown + : Required extends string[] + ? string extends Required[number] + ? unknown + : Required[number] + : unknown, + AdditionalProperties + > : never; type ResolveAllOf = Tuple extends [] diff --git a/test.ts b/test.ts index 8dcb2cb..805bc17 100644 --- a/test.ts +++ b/test.ts @@ -95,6 +95,14 @@ assert( _ as { a: number; b: string } ); +assert( + _ as AsTyped<{ + required: ["a", "b"]; + properties: { a: { type: "number" }; b: { type: "string" } }; + }>, + _ as { a: number; b: string } +); + assert( _ as AsTyped<{ type: "object"; additionalProperties: { type: "number" } }>, _ as { [key: string]: number }