Skip to content

Commit a80d8f3

Browse files
authored
Revision 0.8.2 (#2)
* Add Array and Optional Combinators * Version
1 parent 32f7b9c commit a80d8f3

File tree

16 files changed

+963
-396
lines changed

16 files changed

+963
-396
lines changed

example/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import { Parse } from './typebox'
88
const T = Parse(`{
99
x: number,
1010
y: number,
11-
z: number
11+
z: number
1212
}`)
1313

14-
console.log(T)
14+
console.dir(T, { depth: 100 })
1515

1616
// ------------------------------------------------------------------
1717
// Example: Expression Parser

example/typebox/parse.ts

+23-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*--------------------------------------------------------------------------
22
3-
@sinclair/typebox
3+
@sinclair/typebox/syntax
44
55
The MIT License (MIT)
66
@@ -26,29 +26,35 @@ THE SOFTWARE.
2626
2727
---------------------------------------------------------------------------*/
2828

29+
import * as Types from '@sinclair/typebox'
2930
import { Static } from '@sinclair/parsebox'
30-
import { CreateType } from '@sinclair/typebox'
31-
import { TSchema, SchemaOptions } from '@sinclair/typebox'
3231
import { Module } from './runtime'
33-
import { Type } from './static'
32+
import { Main } from './static'
3433

35-
/** `[Experimental]` Parses a TypeScript type annotation as an inferred TypeBox type */
36-
export function Parse<Code extends string, Context extends Record<PropertyKey, TSchema> = {}>(context: Context, code: Code, options?: SchemaOptions): Static.Parse<Type, Code, Context>[0]
37-
/** `[Experimental]` Parses a TypeScript type annotation as an inferred TypeBox type */
38-
export function Parse<Code extends string>(code: Code, options?: SchemaOptions): Static.Parse<Type, Code, {}>[0]
39-
/** `[Experimental]` Parses a TypeScript type annotation as an inferred TypeBox type */
34+
/** `[Syntax]` Infers a TypeBox type from TypeScript syntax. */
35+
export type StaticParseAsSchema<Context extends Record<PropertyKey, Types.TSchema>, Code extends string> = Static.Parse<Main, Code, Context>[0]
36+
37+
/** `[Syntax]` Infers a TypeScript type from TypeScript syntax. */
38+
export type StaticParseAsType<Context extends Record<PropertyKey, Types.TSchema>, Code extends string> = StaticParseAsSchema<Context, Code> extends infer Type extends Types.TSchema ? Types.StaticDecode<Type> : undefined
39+
40+
/** `[Syntax]` Parses a TypeBox type from TypeScript syntax. */
41+
export function Parse<Context extends Record<PropertyKey, Types.TSchema>, Code extends string>(context: Context, code: Code, options?: Types.SchemaOptions): StaticParseAsSchema<Context, Code>
42+
/** `[Syntax]` Parses a TypeBox type from TypeScript syntax. */
43+
export function Parse<Code extends string>(code: Code, options?: Types.SchemaOptions): StaticParseAsSchema<{}, Code>
44+
/** `[Syntax]` Parses a TypeBox type from TypeScript syntax. */
4045
export function Parse(...args: any[]): never {
4146
return ParseOnly.apply(null, args as never) as never
4247
}
4348

44-
/** `[Experimental]` Parses a TypeScript type annotation as TSchema */
45-
export function ParseOnly<Code extends string, Context extends Record<PropertyKey, TSchema> = {}>(context: Context, code: Code, options?: SchemaOptions): TSchema | undefined
46-
/** `[Experimental]` Parses a TypeScript type annotation as TSchema */
47-
export function ParseOnly<Code extends string>(code: Code, options?: SchemaOptions): TSchema | undefined
48-
/** `[Experimental]` Parses a TypeScript type annotation as TSchema */
49-
export function ParseOnly(...args: any[]): TSchema | undefined {
49+
/** `[Syntax]` Parses a TypeBox TSchema from TypeScript syntax. This function does not infer the type. */
50+
export function ParseOnly<Context extends Record<PropertyKey, Types.TSchema>, Code extends string>(context: Context, code: Code, options?: Types.SchemaOptions): Types.TSchema | undefined
51+
/** `[Syntax]` Parses a TypeBox TSchema from TypeScript syntax */
52+
export function ParseOnly<Code extends string>(code: Code, options?: Types.SchemaOptions): Types.TSchema | undefined
53+
/** `[Syntax]` Parses a TypeBox TSchema from TypeScript syntax. This function does not infer the type. */
54+
export function ParseOnly(...args: any[]): Types.TSchema | undefined {
5055
const withContext = typeof args[0] === 'string' ? false : true
5156
const [context, code, options] = withContext ? [args[0], args[1], args[2] || {}] : [{}, args[0], args[1] || {}]
52-
const type = Module.Parse('Type', code, context)[0] as TSchema | undefined
53-
return (type !== undefined ? CreateType(type, options) : undefined) as never
57+
const type = Module.Parse('Main', code, context)[0] as Types.TSchema | undefined
58+
// Note: Parsing may return either a ModuleInstance or Type. We only apply options on the Type.
59+
return Types.KindGuard.IsSchema(type) ? Types.CloneType(type, options) : type
5460
}

0 commit comments

Comments
 (0)