@sinclair/typebox
Advanced tools
Comparing version 0.34.23 to 0.34.24
import { Runtime } from '../parser/index'; | ||
import * as t from '../type/index'; | ||
export declare const Module: Runtime.Module<{ | ||
GenericArgumentList: Runtime.IUnion<unknown[]>; | ||
GenericArguments: Runtime.ITuple<{}>; | ||
Literal: Runtime.IUnion<t.TLiteral<string> | t.TLiteral<number> | t.TLiteral<boolean>>; | ||
@@ -5,0 +7,0 @@ Keyword: Runtime.IUnion<t.TAny | t.TBoolean | t.TBigInt | t.TNever | t.TString | t.TNumber | t.TInteger | t.TNull | t.TSymbol | t.TUndefined | t.TUnknown | t.TVoid>; |
@@ -43,2 +43,40 @@ "use strict"; | ||
// ------------------------------------------------------------------ | ||
// GenericArgumentList | ||
// ------------------------------------------------------------------ | ||
// prettier-ignore | ||
const GenericArgumentListMapping = (results) => { | ||
return (results.length === 3 ? [results[0], ...results[2]] : | ||
results.length === 2 ? [results[0]] : | ||
results.length === 1 ? [results[0]] : | ||
[]); | ||
}; | ||
// prettier-ignore | ||
const GenericArgumentList = index_1.Runtime.Union([ | ||
index_1.Runtime.Tuple([index_1.Runtime.Ident(), index_1.Runtime.Const(Comma), index_1.Runtime.Ref('GenericArgumentList')]), | ||
index_1.Runtime.Tuple([index_1.Runtime.Ident(), index_1.Runtime.Const(Comma)]), | ||
index_1.Runtime.Tuple([index_1.Runtime.Ident()]), | ||
index_1.Runtime.Tuple([]), | ||
], (results) => GenericArgumentListMapping(results)); | ||
// ------------------------------------------------------------------ | ||
// GenericArguments | ||
// ------------------------------------------------------------------ | ||
// prettier-ignore | ||
const GenericArgumentsContext = (args) => { | ||
return args.reduce((result, arg, index) => { | ||
return { ...result, [arg]: t.Argument(index) }; | ||
}, {}); | ||
}; | ||
// prettier-ignore | ||
const GenericArgumentsMapping = (results) => { | ||
return results.length === 3 | ||
? GenericArgumentsContext(results[1]) | ||
: {}; | ||
}; | ||
// prettier-ignore | ||
const GenericArguments = index_1.Runtime.Tuple([ | ||
index_1.Runtime.Const(LAngle), | ||
index_1.Runtime.Ref('GenericArgumentList'), | ||
index_1.Runtime.Const(RAngle), | ||
], results => GenericArgumentsMapping(results)); | ||
// ------------------------------------------------------------------ | ||
// GenericReference | ||
@@ -619,2 +657,7 @@ // ------------------------------------------------------------------ | ||
// ---------------------------------------------------------------- | ||
// Generic Arguments | ||
// ---------------------------------------------------------------- | ||
GenericArgumentList, | ||
GenericArguments, | ||
// ---------------------------------------------------------------- | ||
// Type Expressions | ||
@@ -621,0 +664,0 @@ // ---------------------------------------------------------------- |
@@ -45,2 +45,23 @@ import { Static } from '../parser/index'; | ||
type Dereference<Context extends t.TProperties, Ref extends string> = (Ref extends keyof Context ? Context[Ref] : t.TRef<Ref>); | ||
interface GenericArgumentListMapping extends Static.IMapping { | ||
output: (this['input'] extends [infer Ident extends string, Comma, infer Rest extends unknown[]] ? [Ident, ...Rest] : this['input'] extends [infer Ident extends string, Comma] ? [Ident] : this['input'] extends [infer Ident extends string] ? [Ident] : [ | ||
]); | ||
} | ||
type GenericArgumentList = Static.Union<[ | ||
Static.Tuple<[Static.Ident, Static.Const<Comma>, GenericArgumentList]>, | ||
Static.Tuple<[Static.Ident, Static.Const<Comma>]>, | ||
Static.Tuple<[Static.Ident]>, | ||
Static.Tuple<[]> | ||
], GenericArgumentListMapping>; | ||
type GenericArgumentsContext<Args extends string[], Result extends t.TProperties = {}> = (Args extends [...infer Left extends string[], infer Right extends string] ? GenericArgumentsContext<Left, Result & { | ||
[_ in Right]: t.TArgument<Left['length']>; | ||
}> : t.Evaluate<Result>); | ||
interface GenericArgumentsMapping extends Static.IMapping { | ||
output: this['input'] extends [LAngle, infer Args extends string[], RAngle] ? GenericArgumentsContext<Args> : never; | ||
} | ||
export type GenericArguments = Static.Tuple<[ | ||
Static.Const<LAngle>, | ||
GenericArgumentList, | ||
Static.Const<RAngle> | ||
], GenericArgumentsMapping>; | ||
interface GenericReferenceMapping extends Static.IMapping { | ||
@@ -47,0 +68,0 @@ output: this['context'] extends t.TProperties ? this['input'] extends [infer Reference extends string, LAngle, infer Args extends t.TSchema[], RAngle] ? t.TInstantiate<Dereference<this['context'], Reference>, Args> : never : never; |
@@ -1,33 +0,31 @@ | ||
import * as Types from '../type/index'; | ||
import * as t from '../type/index'; | ||
import { Static } from '../parser/index'; | ||
import { Type } from './static'; | ||
/** Parses a TSchema type from TypeScript syntax but does not infer schematics */ | ||
export declare function NoInfer<Context extends Record<PropertyKey, Types.TSchema>, Code extends string>(context: Context, code: Code, options?: Types.SchemaOptions): Types.TSchema | undefined; | ||
/** Parses a TSchema type from TypeScript syntax but does not infer schematics */ | ||
export declare function NoInfer<Code extends string>(code: Code, options?: Types.SchemaOptions): Types.TSchema | undefined; | ||
/** Infers a TSchema type from TypeScript syntax. */ | ||
export type TSyntax<Context extends Record<PropertyKey, Types.TSchema>, Code extends string> = (Static.Parse<Type, Code, Context> extends [infer Type extends Types.TSchema, string] ? Type : Types.TNever); | ||
/** Parses a TSchema type from TypeScript syntax */ | ||
export declare function Syntax<Context extends Record<PropertyKey, Types.TSchema>, Code extends string>(context: Context, code: Code, options?: Types.SchemaOptions): TSyntax<Context, Code>; | ||
/** Parses a TSchema type from TypeScript syntax */ | ||
export declare function Syntax<Code extends string>(code: Code, options?: Types.SchemaOptions): TSyntax<{}, Code>; | ||
import { Type, GenericArguments } from './static'; | ||
type TParseSyntax<Context extends Record<PropertyKey, t.TSchema>, Code extends string> = (Static.Parse<GenericArguments, Code, {}> extends [infer Args extends t.TProperties, infer Rest extends string] ? Static.Parse<Type, Rest, Context & Args> : Static.Parse<Type, Code, Context>); | ||
/** Parses a TypeScript annotation into a TypeBox type but does not infer schematics */ | ||
export declare function NoInfer<Context extends Record<PropertyKey, t.TSchema>, Code extends string>(context: Context, code: Code, options?: t.SchemaOptions): t.TSchema; | ||
/** Parses a TypeScript annotation into a TypeBox type but does not infer schematics */ | ||
export declare function NoInfer<Code extends string>(code: Code, options?: t.SchemaOptions): t.TSchema; | ||
/** Parses a TypeScript annotation into a TypeBox type */ | ||
export type TSyntax<Context extends Record<PropertyKey, t.TSchema>, Code extends string> = (TParseSyntax<Context, Code> extends [infer Type extends t.TSchema, string] ? Type : t.TNever); | ||
/** Parses a TypeScript annotation into a TypeBox type */ | ||
export declare function Syntax<Context extends Record<PropertyKey, t.TSchema>, Annotation extends string>(context: Context, annotation: Annotation, options?: t.SchemaOptions): TSyntax<Context, Annotation>; | ||
/** Parses a TypeScript annotation into a TypeBox type */ | ||
export declare function Syntax<Annotation extends string>(annotation: Annotation, options?: t.SchemaOptions): TSyntax<{}, Annotation>; | ||
/** | ||
* Parses a TSchema type from Syntax. | ||
* @deprecated Use Syntax() function | ||
*/ | ||
export declare function Parse<Context extends Record<PropertyKey, Types.TSchema>, Code extends string>(context: Context, code: Code, options?: Types.SchemaOptions): TSyntax<Context, Code>; | ||
export declare function Parse<Context extends Record<PropertyKey, t.TSchema>, Annotation extends string>(context: Context, annotation: Annotation, options?: t.SchemaOptions): TSyntax<Context, Annotation>; | ||
/** | ||
* Parses a TSchema type from Syntax. | ||
* @deprecated Use Syntax() function | ||
*/ | ||
export declare function Parse<Code extends string>(code: Code, options?: Types.SchemaOptions): TSyntax<{}, Code>; | ||
export declare function Parse<Annotation extends string>(annotation: Annotation, options?: t.SchemaOptions): TSyntax<{}, Annotation>; | ||
/** | ||
* Parses a TSchema from TypeScript Syntax | ||
* @deprecated Use NoInfer() function | ||
*/ | ||
export declare function ParseOnly<Context extends Record<PropertyKey, Types.TSchema>, Code extends string>(context: Context, code: Code, options?: Types.SchemaOptions): Types.TSchema | undefined; | ||
export declare function ParseOnly<Context extends Record<PropertyKey, t.TSchema>, Code extends string>(context: Context, code: Code, options?: t.SchemaOptions): t.TSchema | undefined; | ||
/** | ||
* Parses a TSchema from TypeScript Syntax | ||
* @deprecated Use NoInfer() function | ||
*/ | ||
export declare function ParseOnly<Code extends string>(code: Code, options?: Types.SchemaOptions): Types.TSchema | undefined; | ||
export declare function ParseOnly<Code extends string>(code: Code, options?: t.SchemaOptions): t.TSchema | undefined; | ||
export {}; |
@@ -8,15 +8,22 @@ "use strict"; | ||
exports.ParseOnly = ParseOnly; | ||
const Types = require("../type/index"); | ||
const t = require("../type/index"); | ||
const runtime_1 = require("./runtime"); | ||
/** Parses a TSchema type from TypeScript syntax but does not infer schematics */ | ||
// prettier-ignore | ||
function ParseSyntax(context, code) { | ||
const results = runtime_1.Module.Parse('GenericArguments', code, {}); // [ArgumentContext, Rest] | ||
return (results.length === 2 | ||
? runtime_1.Module.Parse('Type', results[1], { ...context, ...results[0] }) | ||
: runtime_1.Module.Parse('Type', code, context)); | ||
} | ||
/** Parses a TypeScript annotation into a TypeBox type but does not infer schematics */ | ||
// prettier-ignore | ||
function NoInfer(...args) { | ||
const withContext = typeof args[0] === 'string' ? false : true; | ||
const [context, code, options] = withContext ? [args[0], args[1], args[2] || {}] : [{}, args[0], args[1] || {}]; | ||
const type = runtime_1.Module.Parse('Type', code, context)[0]; | ||
return Types.KindGuard.IsSchema(type) | ||
? Types.CloneType(type, options) | ||
: Types.Never(options); | ||
const result = ParseSyntax(context, code)[0]; | ||
return t.KindGuard.IsSchema(result) | ||
? t.CloneType(result, options) | ||
: t.Never(options); | ||
} | ||
/** Parses a TSchema type from TypeScript syntax */ | ||
/** Parses a TypeScript annotation into a TypeBox type */ | ||
function Syntax(...args) { | ||
@@ -26,3 +33,2 @@ return NoInfer.apply(null, args); | ||
/** | ||
* Parses a TSchema type from Syntax. | ||
* @deprecated Use Syntax() function | ||
@@ -34,3 +40,2 @@ */ | ||
/** | ||
* Parses a TSchema from TypeScript Syntax | ||
* @deprecated Use NoInfer() function | ||
@@ -37,0 +42,0 @@ */ |
import { type TSchema } from '../schema/index'; | ||
import { type TArgument } from '../argument/index'; | ||
import { type TNever } from '../never/index'; | ||
import { type TUnknown } from '../unknown/index'; | ||
import { type TReadonlyOptional } from '../readonly-optional/index'; | ||
@@ -27,5 +27,5 @@ import { type TReadonly } from '../readonly/index'; | ||
type TFromPromise<Args extends TSchema[], Type extends TSchema, Result extends TPromise = TPromise<TFromType<Args, Type>>> = Result; | ||
type TFromObject<Args extends TSchema[], Properties extends TProperties, Result extends TObject = TObject<TFromProperties<Args, Properties>>> = Result; | ||
type TFromObject<Args extends TSchema[], Properties extends TProperties, Result extends TProperties = TFromProperties<Args, Properties>> = TObject<Result>; | ||
type TFromRecord<Args extends TSchema[], Key extends TSchema, Value extends TSchema, MappedKey extends TSchema = TFromType<Args, Key>, MappedValue extends TSchema = TFromType<Args, Value>, Result extends TSchema = TRecordOrObject<MappedKey, MappedValue>> = Result; | ||
type TFromArgument<Args extends TSchema[], Index extends number> = (Index extends keyof Args ? Args[Index] : TNever); | ||
type TFromArgument<Args extends TSchema[], Index extends number, Result extends TSchema = Index extends keyof Args[Index] ? Args[Index] : TUnknown> = Result; | ||
type TFromProperty<Args extends TSchema[], Type extends TSchema, IsReadonly extends boolean = Type extends TReadonly<Type> ? true : false, IsOptional extends boolean = Type extends TOptional<Type> ? true : false, Mapped extends TSchema = TFromType<Args, Type>, Result extends TSchema = ([ | ||
@@ -32,0 +32,0 @@ IsReadonly, |
@@ -7,7 +7,8 @@ "use strict"; | ||
const type_1 = require("../clone/type"); | ||
const index_1 = require("../never/index"); | ||
const index_1 = require("../unknown/index"); | ||
const index_2 = require("../readonly-optional/index"); | ||
const index_3 = require("../readonly/index"); | ||
const index_4 = require("../optional/index"); | ||
const index_5 = require("../record/index"); | ||
const index_5 = require("../object/index"); | ||
const index_6 = require("../record/index"); | ||
const ValueGuard = require("../guard/value"); | ||
@@ -66,10 +67,10 @@ const KindGuard = require("../guard/kind"); | ||
function FromObject(args, type) { | ||
type.properties = FromProperties(args, type.properties); | ||
return type; | ||
const properties = FromProperties(args, type.properties); | ||
return { ...type, ...(0, index_5.Object)(properties) }; // retain options | ||
} | ||
// prettier-ignore | ||
function FromRecord(args, type) { | ||
const mappedKey = FromType(args, (0, index_5.RecordKey)(type)); | ||
const mappedValue = FromType(args, (0, index_5.RecordValue)(type)); | ||
const result = (0, index_5.Record)(mappedKey, mappedValue); | ||
const mappedKey = FromType(args, (0, index_6.RecordKey)(type)); | ||
const mappedValue = FromType(args, (0, index_6.RecordValue)(type)); | ||
const result = (0, index_6.Record)(mappedKey, mappedValue); | ||
return { ...type, ...result }; // retain options | ||
@@ -79,3 +80,3 @@ } | ||
function FromArgument(args, argument) { | ||
return argument.index in args ? args[argument.index] : (0, index_1.Never)(); | ||
return argument.index in args ? args[argument.index] : (0, index_1.Unknown)(); | ||
} | ||
@@ -82,0 +83,0 @@ // prettier-ignore |
{ | ||
"name": "@sinclair/typebox", | ||
"version": "0.34.23", | ||
"version": "0.34.24", | ||
"description": "Json Schema Type Builder with Static Type Resolution for TypeScript", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
1723924
28718
1842