diff --git a/package.json b/package.json index 290e0e0..1870f8d 100644 --- a/package.json +++ b/package.json @@ -70,6 +70,6 @@ "access": "public" }, "dependencies": { - "@the-minimal/types": "0.3.6" + "@the-minimal/types": "0.4.0" } } diff --git a/src/assertions/and/types.ts b/src/assertions/and/types.ts index db894a2..bc79fb2 100644 --- a/src/assertions/and/types.ts +++ b/src/assertions/and/types.ts @@ -3,7 +3,7 @@ export type Intersection<$Values extends unknown[]> = $Values extends [ infer $Head, ...infer $Tail, ] - ? $Tail extends [infer _1, ...infer _2] + ? $Tail extends [infer _1] ? $Head & Intersection<$Tail> : $Head : never; diff --git a/src/assertions/and2/index.ts b/src/assertions/and2/index.ts index 6dfb395..880ec9a 100644 --- a/src/assertions/and2/index.ts +++ b/src/assertions/and2/index.ts @@ -1,8 +1,4 @@ -import type { - Assertion, - InferAssertion, - UnknownAssertion, -} from "@the-minimal/types"; +import type { Assertion } from "@the-minimal/types"; /** * Checks that both assertions pass. @@ -20,10 +16,10 @@ import type { * ``` */ export const and2 = - <$Assertion1 extends UnknownAssertion, $Assertion2 extends UnknownAssertion>( - assertion1: $Assertion1, - assertion2: $Assertion2, - ): Assertion & InferAssertion<$Assertion2>> => + <$Value1, $Value2>( + assertion1: Assertion<$Value1>, + assertion2: Assertion<$Value2>, + ): Assertion<$Value1 & $Value2> => (v: unknown) => { assertion1(v); assertion2(v); diff --git a/src/assertions/and3/index.ts b/src/assertions/and3/index.ts index d8ce536..955d762 100644 --- a/src/assertions/and3/index.ts +++ b/src/assertions/and3/index.ts @@ -1,8 +1,4 @@ -import type { - Assertion, - InferAssertion, - UnknownAssertion, -} from "@the-minimal/types"; +import type { Assertion } from "@the-minimal/types"; /** * Checks that all three assertions pass. @@ -22,19 +18,11 @@ import type { * ``` */ export const and3 = - < - $Assertion1 extends UnknownAssertion, - $Assertion2 extends UnknownAssertion, - $Assertion3 extends UnknownAssertion, - >( - assertion1: $Assertion1, - assertion2: $Assertion2, - assertion3: $Assertion3, - ): Assertion< - InferAssertion<$Assertion1> & - InferAssertion<$Assertion2> & - InferAssertion<$Assertion3> - > => + <$Value1, $Value2, $Value3>( + assertion1: Assertion<$Value1>, + assertion2: Assertion<$Value2>, + assertion3: Assertion<$Value3>, + ): Assertion<$Value1 & $Value2 & $Value3> => (v: unknown) => { assertion1(v); assertion2(v); diff --git a/src/assertions/array/index.ts b/src/assertions/array/index.ts index c6d8922..46ba312 100644 --- a/src/assertions/array/index.ts +++ b/src/assertions/array/index.ts @@ -1,5 +1,5 @@ import { isArray } from "@assertions/isArray"; -import type { UnknownAssertion } from "@the-minimal/types"; +import type { Assertion } from "@the-minimal/types"; /** * Checks that assertion passes for each element of the array. @@ -15,7 +15,7 @@ import type { UnknownAssertion } from "@the-minimal/types"; * ``` */ export const array = - <$Assertion extends UnknownAssertion>(assertion: $Assertion) => + <$Value>(assertion: Assertion<$Value>): Assertion<$Value[]> => (v: unknown) => { isArray(v); diff --git a/src/assertions/nullable/index.ts b/src/assertions/nullable/index.ts index 84c094a..b172bfd 100644 --- a/src/assertions/nullable/index.ts +++ b/src/assertions/nullable/index.ts @@ -1,9 +1,4 @@ -import type { - Assertion, - InferAssertion, - Nullable, - UnknownAssertion, -} from "@the-minimal/types"; +import type { Assertion, Nullable } from "@the-minimal/types"; /** * Checks if the assertion passes or if the value is null. @@ -20,8 +15,6 @@ import type { * ``` */ export const nullable = - <$Assertion extends UnknownAssertion>( - assertion: $Assertion, - ): Assertion>> => + <$Value>(assertion: Assertion<$Value>): Assertion> => (v: unknown) => v === null || (assertion as any)(v); diff --git a/src/assertions/nullish/index.ts b/src/assertions/nullish/index.ts index 2863e61..151072e 100644 --- a/src/assertions/nullish/index.ts +++ b/src/assertions/nullish/index.ts @@ -1,9 +1,4 @@ -import type { - Assertion, - InferAssertion, - Nullish, - UnknownAssertion, -} from "@the-minimal/types"; +import type { Assertion, Nullish } from "@the-minimal/types"; /** * Checks if the assertion passes or if the value is null or undefined. @@ -21,8 +16,6 @@ import type { * ``` */ export const nullish = - <$Assertion extends UnknownAssertion>( - assertion: $Assertion, - ): Assertion>> => + <$Value>(assertion: Assertion<$Value>): Assertion> => (v: unknown) => v === null || v === undefined || (assertion as any)(v); diff --git a/src/assertions/object/index.ts b/src/assertions/object/index.ts index f850cc5..03d0073 100644 --- a/src/assertions/object/index.ts +++ b/src/assertions/object/index.ts @@ -1,9 +1,5 @@ import { isObject } from "@assertions/isObject"; -import type { - Assertion, - InferAssertion, - UnknownAssertion, -} from "@the-minimal/types"; +import type { Assertion } from "@the-minimal/types"; /** * Checks that assertion passes for each key/value of the object. @@ -29,11 +25,11 @@ import type { * }); // passes * ``` */ -export const object = <$Schema extends Record>( - schema: $Schema, -): Assertion<{ - [$Key in keyof $Schema]: InferAssertion<$Schema[$Key]>; -}> => { +export const object = <$Schema extends Record>( + schema: { + [$Key in keyof $Schema]: Assertion<$Schema[$Key]>; + }, +): Assertion<$Schema> => { const keys = Object.keys(schema); return (v: unknown) => { diff --git a/src/assertions/optional/index.ts b/src/assertions/optional/index.ts index f9e6c64..c3806b9 100644 --- a/src/assertions/optional/index.ts +++ b/src/assertions/optional/index.ts @@ -1,9 +1,4 @@ -import type { - Assertion, - InferAssertion, - Optional, - UnknownAssertion, -} from "@the-minimal/types"; +import type { Assertion, Optional } from "@the-minimal/types"; /** * Checks if the assertion passes or if the value is undefined. @@ -20,8 +15,6 @@ import type { * ``` */ export const optional = - <$Assertion extends UnknownAssertion>( - assertion: $Assertion, - ): Assertion>> => + <$Value>(assertion: Assertion<$Value>): Assertion> => (v: unknown) => v === undefined || (assertion as any)(v); diff --git a/src/assertions/or/index.ts b/src/assertions/or/index.ts index 0002133..58b88b1 100644 --- a/src/assertions/or/index.ts +++ b/src/assertions/or/index.ts @@ -1,9 +1,5 @@ import { error } from "@error"; -import type { - Assertion, - InferAssertion, - UnknownAssertion, -} from "@the-minimal/types"; +import type { Assertion } from "@the-minimal/types"; /** * Checks if one of the assertions passes. @@ -26,13 +22,11 @@ import type { * ``` */ export const or = - ( - assertions: $Assertions, - ): Assertion< - { - [$Key in keyof $Assertions]: InferAssertion<$Assertions[$Key]>; - }[number] - > => + ( + assertions: { + [$Key in keyof $Values]: Assertion<$Values[$Key]>; + }, + ): Assertion<$Values[number]> => (v: unknown) => { for (let i = 0; i < assertions.length; ++i) { try { diff --git a/src/assertions/or2/index.ts b/src/assertions/or2/index.ts index 52842a1..1b34cce 100644 --- a/src/assertions/or2/index.ts +++ b/src/assertions/or2/index.ts @@ -1,8 +1,4 @@ -import type { - Assertion, - InferAssertion, - UnknownAssertion, -} from "@the-minimal/types"; +import type { Assertion } from "@the-minimal/types"; /** * Checks if one of two assertions passes. @@ -23,10 +19,10 @@ import type { * ``` */ export const or2 = - <$Assertion1 extends UnknownAssertion, $Assertion2 extends UnknownAssertion>( - assertion1: $Assertion1, - assertion2: $Assertion2, - ): Assertion | InferAssertion<$Assertion2>> => + <$Value1, $Value2>( + assertion1: Assertion<$Value1>, + assertion2: Assertion<$Value2>, + ): Assertion<$Value1 | $Value2> => (v: unknown) => { try { assertion1(v); diff --git a/src/assertions/or3/index.ts b/src/assertions/or3/index.ts index b2fd5b4..3e55fce 100644 --- a/src/assertions/or3/index.ts +++ b/src/assertions/or3/index.ts @@ -1,8 +1,4 @@ -import type { - Assertion, - InferAssertion, - UnknownAssertion, -} from "@the-minimal/types"; +import type { Assertion } from "@the-minimal/types"; /** * Checks if one of two assertions passes. @@ -23,19 +19,11 @@ import type { * ``` */ export const or3 = - < - $Assertion1 extends UnknownAssertion, - $Assertion2 extends UnknownAssertion, - $Assertion3 extends UnknownAssertion, - >( - assertion1: $Assertion1, - assertion2: $Assertion2, - assertion3: $Assertion3, - ): Assertion< - | InferAssertion<$Assertion1> - | InferAssertion<$Assertion2> - | InferAssertion<$Assertion3> - > => + <$Value1, $Value2, $Value3>( + assertion1: Assertion<$Value1>, + assertion2: Assertion<$Value2>, + assertion3: Assertion<$Value3>, + ): Assertion<$Value1 | $Value2 | $Value3> => (v: unknown) => { try { assertion1(v); diff --git a/src/assertions/tuple/index.ts b/src/assertions/tuple/index.ts index 5ba4f6c..063fbf5 100644 --- a/src/assertions/tuple/index.ts +++ b/src/assertions/tuple/index.ts @@ -1,9 +1,5 @@ import { isArray } from "@assertions/isArray"; -import type { - Assertion, - InferAssertion, - UnknownAssertion, -} from "@the-minimal/types"; +import type { Assertion } from "@the-minimal/types"; /** * Checks that assertion passes for each element of the tuple. @@ -20,11 +16,11 @@ import type { * ``` */ export const tuple = - ( - assertions: $Assertions, - ): Assertion<{ - [$Key in keyof $Assertions]: InferAssertion<$Assertions[$Key]>; - }> => + ( + assertions: { + [$Key in keyof $Values]: Assertion<$Values[$Key]>; + }, + ): Assertion<$Values> => (v: unknown) => { isArray(v); diff --git a/src/utils.ts b/src/utils.ts index 627e608..d646eaa 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,26 +1,19 @@ -import type { UnknownAssertion } from "@the-minimal/types"; -import type { Infer } from "@types"; +import type { Assertion } from "@the-minimal/types"; -export function assert<$Assertion extends UnknownAssertion>( - assertion: $Assertion, +export function assert<$Value>( + assertion: Assertion<$Value>, value: unknown, -): asserts value is Infer<$Assertion> { +): asserts value is $Value { (assertion as any)(value); } -export function parse<$Assertion extends UnknownAssertion>( - assertion: $Assertion, - value: unknown, -) { +export function parse<$Value>(assertion: Assertion<$Value>, value: unknown) { (assertion as any)(value); - return value as Infer<$Assertion>; + return value as $Value; } -export function is<$Assertion extends UnknownAssertion>( - assertion: $Assertion, - value: unknown, -): value is Infer<$Assertion> { +export function is<$Value>(assertion: $Value, value: unknown): value is $Value { try { (assertion as any)(value);