Revision 0.8.2 (#11)

* Ensure Type Evaluation at Mapping Signature

* Version
This commit is contained in:
sinclairzx81
2025-01-26 16:29:26 +09:00
committed by GitHub
parent 3c03dbf43d
commit 6a7f983e94
6 changed files with 24 additions and 20 deletions

4
package-lock.json generated
View File

@@ -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",

View File

@@ -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",

View File

@@ -35,13 +35,14 @@ import * as t from '@sinclair/typebox'
/** Creates a TypeBox type from Syntax or another Type */
// prettier-ignore
export type TTypeBox<Type extends object | string> = (
export type TTypeBox<Type extends object | string, Result = (
Guard.TIsSyntax<Type> extends true ? TTypeBoxFromSyntax<Type> :
Guard.TIsTypeBox<Type> extends true ? TTypeBoxFromTypeBox<Type> :
Guard.TIsValibot<Type> extends true ? TTypeBoxFromValibot<Type> :
Guard.TIsZod<Type> extends true ? TTypeBoxFromZod<Type> :
t.TNever
)
)> = Result
/** Creates a TypeBox type from Syntax or another Type */
// prettier-ignore
export function TypeBox<Type extends object | string>(type: Type): TTypeBox<Type> {
@@ -58,6 +59,6 @@ export function TypeBox<Type extends object | string>(type: Type): TTypeBox<Type
* Creates a TypeBox type from Syntax or another Type
* @deprecated Use TypeBox() export instead
*/
export function Box<Type extends object | string>(type: Type): TTypeBox<Type> {
return TypeBox(type)
export function Box<Type extends object | string, Mapped = TTypeBox<Type>, Result extends Mapped = Mapped>(type: Type): Result {
return TypeBox(type) as never
}

View File

@@ -96,7 +96,7 @@ function FromFunction(type: t.TFunction): c.BaseSchema {
// ------------------------------------------------------------------
// Integer
// ------------------------------------------------------------------
type TFromInteger = v.NumberSchema<c.BaseError>
type TFromInteger<Result = v.NumberSchema<c.BaseError>> = 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 t.TSchema[], Result extends c.BaseSchema[] = []> = Types extends [infer Left extends t.TSchema, ...infer Right extends t.TSchema[]]
? TFromIntersect<Right, [...Result, TFromType<Left>]>
: v.IntersectSchema<Result, any>
// prettier-ignore
type TFromIntersect<Types extends t.TSchema[], Result extends c.BaseSchema[] = []> = (
Types extends [infer Left extends t.TSchema, ...infer Right extends t.TSchema[]]
? TFromIntersect<Right, [...Result, TFromType<Left>]>
: v.IntersectSchema<Result, any>
)
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<Value extends t.TLiteralValue> = v.LiteralSchema<Value, any>
type TFromLiteral<Value extends t.TLiteralValue, Result = v.LiteralSchema<Value, undefined>> = 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<Type extends object | string> = (
export type TValibotFromTypeBox<Type extends object | string, Result extends c.BaseSchema = (
Type extends t.TSchema ? TFromType<Type> : v.NeverSchema<c.BaseError>
)
)> = Result
// prettier-ignore
export function ValibotFromTypeBox<Type extends object | string>(type: Type): TValibotFromTypeBox<Type> {
return (t.KindGuard.IsSchema(type) ? FromType(type) : v.never()) as never

View File

@@ -36,16 +36,16 @@ import * as c from './common'
/** Creates a Valibot type from Syntax or another Type */
// prettier-ignore
export type TValibot<Type extends object | string> = (
export type TValibot<Type extends object | string, Result = (
Guard.TIsSyntax<Type> extends true ? TValibotFromSyntax<Type> :
Guard.TIsTypeBox<Type> extends true ? TValibotFromTypeBox<Type> :
Guard.TIsValibot<Type> extends true ? TValibotFromValibot<Type> :
Guard.TIsZod<Type> extends true ? TValibotFromZod<Type> :
v.NeverSchema<c.BaseError>
)
)> = Result
/** Creates a Valibot type from Syntax or another Type */
// prettier-ignore
export function Valibot<Type extends object | string, Result = TValibot<Type>>(type: Type): Result {
export function Valibot<Type extends object | string, Mapped = TValibot<Type>, Result extends Mapped = Mapped>(type: Type): Result {
return (
Guard.IsSyntax(type) ? ValibotFromSyntax(type) :
Guard.IsTypeBox(type) ? ValibotFromTypeBox(type) :

View File

@@ -35,17 +35,17 @@ import * as z from 'zod'
/** Creates a Zod type from Syntax or another Type */
// prettier-ignore
export type TZod<Type extends object | string> = (
export type TZod<Type extends object | string, Result = (
Guard.TIsSyntax<Type> extends true ? TZodFromSyntax<Type> :
Guard.TIsTypeBox<Type> extends true ? TZodFromTypeBox<Type> :
Guard.TIsValibot<Type> extends true ? TZodFromValibot<Type> :
Guard.TIsZod<Type> extends true ? TZodFromZod<Type> :
z.ZodNever
)
)> = Result
/** Creates a Zod type from Syntax or another Type */
// prettier-ignore
export function Zod<Type extends object | string, Result = TZod<Type>>(type: Type): Result {
export function Zod<Type extends object | string, Mapped = TZod<Type>, Result extends Mapped = Mapped>(type: Type): Result {
return (
Guard.IsSyntax(type) ? ZodFromSyntax(type) :
Guard.IsTypeBox(type) ? ZodFromTypeBox(type) :