diff --git a/package-lock.json b/package-lock.json index d157881..4be964a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@sinclair/typemap", - "version": "0.8.1", + "version": "0.8.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@sinclair/typemap", - "version": "0.8.1", + "version": "0.8.2", "license": "MIT", "devDependencies": { "@arethetypeswrong/cli": "^0.17.2", diff --git a/package.json b/package.json index bf958ee..37be179 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@sinclair/typemap", - "version": "0.8.1", + "version": "0.8.2", "description": "Uniform Syntax, Mapping and Compiler Library for TypeBox, Valibot and Zod", "author": "sinclairzx81", "license": "MIT", diff --git a/src/typebox/typebox.ts b/src/typebox/typebox.ts index 6e5bb69..ac387aa 100644 --- a/src/typebox/typebox.ts +++ b/src/typebox/typebox.ts @@ -35,13 +35,14 @@ import * as t from '@sinclair/typebox' /** Creates a TypeBox type from Syntax or another Type */ // prettier-ignore -export type TTypeBox = ( +export type TTypeBox extends true ? TTypeBoxFromSyntax : Guard.TIsTypeBox extends true ? TTypeBoxFromTypeBox : Guard.TIsValibot extends true ? TTypeBoxFromValibot : Guard.TIsZod extends true ? TTypeBoxFromZod : t.TNever -) +)> = Result + /** Creates a TypeBox type from Syntax or another Type */ // prettier-ignore export function TypeBox(type: Type): TTypeBox { @@ -58,6 +59,6 @@ export function TypeBox(type: Type): TTypeBox(type: Type): TTypeBox { - return TypeBox(type) +export function Box, Result extends Mapped = Mapped>(type: Type): Result { + return TypeBox(type) as never } diff --git a/src/valibot/valibot-from-typebox.ts b/src/valibot/valibot-from-typebox.ts index e8e653a..4c18bf7 100644 --- a/src/valibot/valibot-from-typebox.ts +++ b/src/valibot/valibot-from-typebox.ts @@ -96,7 +96,7 @@ function FromFunction(type: t.TFunction): c.BaseSchema { // ------------------------------------------------------------------ // Integer // ------------------------------------------------------------------ -type TFromInteger = v.NumberSchema +type TFromInteger> = Result function FromInteger(type: t.TInteger): c.BaseSchema { const { exclusiveMaximum, exclusiveMinimum, minimum, maximum, multipleOf } = type const constraints = CreateConstraints(type, [v.integer()]) @@ -110,9 +110,12 @@ function FromInteger(type: t.TInteger): c.BaseSchema { // ------------------------------------------------------------------ // Intersect // ------------------------------------------------------------------ -type TFromIntersect = Types extends [infer Left extends t.TSchema, ...infer Right extends t.TSchema[]] - ? TFromIntersect]> - : v.IntersectSchema +// prettier-ignore +type TFromIntersect = ( + Types extends [infer Left extends t.TSchema, ...infer Right extends t.TSchema[]] + ? TFromIntersect]> + : v.IntersectSchema +) function FromIntersect(type: t.TIntersect): c.BaseSchema { const schemas = type.allOf.map((schema) => FromType(schema)) return CreateType(v.intersect(schemas), CreateConstraints(type)) @@ -120,7 +123,7 @@ function FromIntersect(type: t.TIntersect): c.BaseSchema { // ------------------------------------------------------------------ // Literal // ------------------------------------------------------------------ -type TFromLiteral = v.LiteralSchema +type TFromLiteral> = Result function FromLiteral(type: t.TLiteral): c.BaseSchema { return CreateType(v.literal(type.const), CreateConstraints(type)) } @@ -385,9 +388,9 @@ function FromType(type: t.TSchema): c.BaseSchema { // ValibotFromTypeBox // ------------------------------------------------------------------ // prettier-ignore -export type TValibotFromTypeBox = ( +export type TValibotFromTypeBox : v.NeverSchema -) +)> = Result // prettier-ignore export function ValibotFromTypeBox(type: Type): TValibotFromTypeBox { return (t.KindGuard.IsSchema(type) ? FromType(type) : v.never()) as never diff --git a/src/valibot/valibot.ts b/src/valibot/valibot.ts index 18b27a7..f780c85 100644 --- a/src/valibot/valibot.ts +++ b/src/valibot/valibot.ts @@ -36,16 +36,16 @@ import * as c from './common' /** Creates a Valibot type from Syntax or another Type */ // prettier-ignore -export type TValibot = ( +export type TValibot extends true ? TValibotFromSyntax : Guard.TIsTypeBox extends true ? TValibotFromTypeBox : Guard.TIsValibot extends true ? TValibotFromValibot : Guard.TIsZod extends true ? TValibotFromZod : v.NeverSchema -) +)> = Result /** Creates a Valibot type from Syntax or another Type */ // prettier-ignore -export function Valibot>(type: Type): Result { +export function Valibot, Result extends Mapped = Mapped>(type: Type): Result { return ( Guard.IsSyntax(type) ? ValibotFromSyntax(type) : Guard.IsTypeBox(type) ? ValibotFromTypeBox(type) : diff --git a/src/zod/zod.ts b/src/zod/zod.ts index cba9229..f8024e6 100644 --- a/src/zod/zod.ts +++ b/src/zod/zod.ts @@ -35,17 +35,17 @@ import * as z from 'zod' /** Creates a Zod type from Syntax or another Type */ // prettier-ignore -export type TZod = ( +export type TZod extends true ? TZodFromSyntax : Guard.TIsTypeBox extends true ? TZodFromTypeBox : Guard.TIsValibot extends true ? TZodFromValibot : Guard.TIsZod extends true ? TZodFromZod : z.ZodNever -) +)> = Result /** Creates a Zod type from Syntax or another Type */ // prettier-ignore -export function Zod>(type: Type): Result { +export function Zod, Result extends Mapped = Mapped>(type: Type): Result { return ( Guard.IsSyntax(type) ? ZodFromSyntax(type) : Guard.IsTypeBox(type) ? ZodFromTypeBox(type) :