diff --git a/src/assertions/and/index.ts b/src/assertions/and/index.ts index 02b48b9..63815cc 100644 --- a/src/assertions/and/index.ts +++ b/src/assertions/and/index.ts @@ -1,4 +1,8 @@ -import type { AndSchema, InferAndSchema } from "@assertions/and/types"; +import type { + AndSchema, + InferAndOutput, + InferAndSchema, +} from "@assertions/and/types"; import type { Assertion } from "@the-minimal/types"; /** @@ -21,10 +25,10 @@ import type { Assertion } from "@the-minimal/types"; */ export const and = ( - assertions: $Schema, - ): Assertion> => + assertions: InferAndSchema<$Schema>, + ): Assertion> => (v) => { for (let i = 0; i < assertions.length; ++i) { - (assertions[i] as any)(v); + ((assertions as any)[i] as any)(v); } }; diff --git a/src/assertions/and/types.d.ts b/src/assertions/and/types.d.ts index 7c3a76b..8e18654 100644 --- a/src/assertions/and/types.d.ts +++ b/src/assertions/and/types.d.ts @@ -1,11 +1,16 @@ -import type { UnknownAssertion } from "@the-minimal/types"; -import type { Infer } from "@types"; +import type { Assertion } from "@the-minimal/types"; -export type AndSchema = Array; +export type AndSchema = Array; -export type InferAndSchema<$Validations extends AndSchema> = - $Validations extends [infer $Head, ...infer $Tail] - ? $Tail extends [infer $1, ...infer $2] - ? Infer<$Head> & InferAndSchema<$Tail> - : Infer<$Head> - : never; +export type InferAndSchema<$Schema extends AndSchema> = { + [$Key in keyof $Schema]: Assertion<$Schema[$Key]>; +}; + +export type InferAndOutput<$Schema extends AndSchema> = $Schema extends [ + infer $Head, + ...infer $Tail, +] + ? $Tail extends [infer $1, ...infer $2] + ? $Head & InferAndOutput<$Tail> + : $Head + : never; diff --git a/src/assertions/object/index.ts b/src/assertions/object/index.ts index 64f2202..16a525a 100644 --- a/src/assertions/object/index.ts +++ b/src/assertions/object/index.ts @@ -1,5 +1,6 @@ import { isObject } from "@assertions/isObject"; import type { InferObjectSchema, ObjectSchema } from "@assertions/object/types"; +import { string } from "@assertions/string"; import type { Assertion } from "@the-minimal/types"; /** @@ -27,8 +28,8 @@ import type { Assertion } from "@the-minimal/types"; * ``` */ export const object = <$Schema extends ObjectSchema>( - schema: $Schema, -): Assertion> => { + schema: InferObjectSchema<$Schema>, +): Assertion<$Schema> => { const keys = Object.keys(schema); return (v) => { diff --git a/src/assertions/object/types.d.ts b/src/assertions/object/types.d.ts index 557022a..2033b3a 100644 --- a/src/assertions/object/types.d.ts +++ b/src/assertions/object/types.d.ts @@ -1,11 +1,8 @@ -import type { - InferAssertion, - Pretty, - UnknownAssertion, -} from "@the-minimal/types"; +import type { ObjectUnknown } from "@assertions/isObject/types"; +import type { Assertion, Pretty } from "@the-minimal/types"; -export type ObjectSchema = Record; +export type ObjectSchema = ObjectUnknown; export type InferObjectSchema<$Schema extends ObjectSchema> = Pretty<{ - [$Key in keyof $Schema]: InferAssertion<$Schema[$Key]>; + [$Key in keyof $Schema]: Assertion<$Schema[$Key]>; }>; diff --git a/src/assertions/or/index.ts b/src/assertions/or/index.ts index 0677535..fb07c6e 100644 --- a/src/assertions/or/index.ts +++ b/src/assertions/or/index.ts @@ -24,8 +24,8 @@ import type { Assertion } from "@the-minimal/types"; */ export const or = ( - validations: $Validations, - ): Assertion> => + validations: InferOrSchema<$Validations>, + ): Assertion<$Validations[number]> => (v) => { for (let i = 0; i < validations.length; ++i) { try { diff --git a/src/assertions/or/types.d.ts b/src/assertions/or/types.d.ts index e77da1c..f7a303d 100644 --- a/src/assertions/or/types.d.ts +++ b/src/assertions/or/types.d.ts @@ -1,7 +1,11 @@ -import type { InferAssertion, UnknownAssertion } from "@the-minimal/types"; +import type { + Assertion, + InferAssertion, + UnknownAssertion, +} from "@the-minimal/types"; -export type OrSchema = Array; +export type OrSchema = Array; export type InferOrSchema<$Schema extends OrSchema> = { - [$Key in keyof $Schema]: InferAssertion<$Schema[$Key]>; -}[number]; + [$Key in keyof $Schema]: Assertion<$Schema[$Key]>; +}; diff --git a/src/assertions/tuple/index.ts b/src/assertions/tuple/index.ts index ab8b622..d4ff460 100644 --- a/src/assertions/tuple/index.ts +++ b/src/assertions/tuple/index.ts @@ -18,8 +18,8 @@ import type { Assertion } from "@the-minimal/types"; */ export const tuple = ( - assertions: $Schema, - ): Assertion> => + assertions: InferTupleSchema<$Schema>, + ): Assertion<$Schema> => (v) => { isArray(v); diff --git a/src/assertions/tuple/types.d.ts b/src/assertions/tuple/types.d.ts index f5e5bd8..969dfbb 100644 --- a/src/assertions/tuple/types.d.ts +++ b/src/assertions/tuple/types.d.ts @@ -1,7 +1,7 @@ -import type { InferAssertion, UnknownAssertion } from "@the-minimal/types"; +import type { Assertion } from "@the-minimal/types"; -export type TupleSchema = Array; +export type TupleSchema = Array; export type InferTupleSchema<$Schema extends TupleSchema> = { - [$Key in keyof $Schema]: InferAssertion<$Schema[$Key]>; + [$Key in keyof $Schema]: Assertion<$Schema[$Key]>; };