Skip to content

Commit

Permalink
feat: allow objects to be specified without "type": "object"
Browse files Browse the repository at this point in the history
  • Loading branch information
lbguilherme committed Sep 7, 2022
1 parent 45c76cc commit 0d67f1d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
34 changes: 17 additions & 17 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,23 +97,6 @@ declare namespace AsTypedInternal {
: AsTypedTupleSchemaWithAdditional<TupleType, Additional>
: SchemaType extends { not: infer T }
? ResolveNot<T>
: 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<Resolve<ValueType>>
: SchemaType extends {
Expand All @@ -134,6 +117,23 @@ declare namespace AsTypedInternal {
? ResolveAllOf<Inner>
: SchemaType extends { if: infer If; then: infer Then; else?: infer Else }
? (Resolve<If> & Resolve<Then>) | Resolve<Else>
: 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> = Tuple extends []
Expand Down
8 changes: 8 additions & 0 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down

0 comments on commit 0d67f1d

Please sign in to comment.