Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revision 0.8.2 #2

Merged
merged 2 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions example/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import { Parse } from './typebox'
const T = Parse(`{
x: number,
y: number,
z: number
z: number
}`)

console.log(T)
console.dir(T, { depth: 100 })

// ------------------------------------------------------------------
// Example: Expression Parser
Expand Down
40 changes: 23 additions & 17 deletions example/typebox/parse.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*--------------------------------------------------------------------------

@sinclair/typebox
@sinclair/typebox/syntax

The MIT License (MIT)

Expand All @@ -26,29 +26,35 @@ THE SOFTWARE.

---------------------------------------------------------------------------*/

import * as Types from '@sinclair/typebox'
import { Static } from '@sinclair/parsebox'
import { CreateType } from '@sinclair/typebox'
import { TSchema, SchemaOptions } from '@sinclair/typebox'
import { Module } from './runtime'
import { Type } from './static'
import { Main } from './static'

/** `[Experimental]` Parses a TypeScript type annotation as an inferred TypeBox type */
export function Parse<Code extends string, Context extends Record<PropertyKey, TSchema> = {}>(context: Context, code: Code, options?: SchemaOptions): Static.Parse<Type, Code, Context>[0]
/** `[Experimental]` Parses a TypeScript type annotation as an inferred TypeBox type */
export function Parse<Code extends string>(code: Code, options?: SchemaOptions): Static.Parse<Type, Code, {}>[0]
/** `[Experimental]` Parses a TypeScript type annotation as an inferred TypeBox type */
/** `[Syntax]` Infers a TypeBox type from TypeScript syntax. */
export type StaticParseAsSchema<Context extends Record<PropertyKey, Types.TSchema>, Code extends string> = Static.Parse<Main, Code, Context>[0]

/** `[Syntax]` Infers a TypeScript type from TypeScript syntax. */
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

/** `[Syntax]` Parses a TypeBox type from TypeScript syntax. */
export function Parse<Context extends Record<PropertyKey, Types.TSchema>, Code extends string>(context: Context, code: Code, options?: Types.SchemaOptions): StaticParseAsSchema<Context, Code>
/** `[Syntax]` Parses a TypeBox type from TypeScript syntax. */
export function Parse<Code extends string>(code: Code, options?: Types.SchemaOptions): StaticParseAsSchema<{}, Code>
/** `[Syntax]` Parses a TypeBox type from TypeScript syntax. */
export function Parse(...args: any[]): never {
return ParseOnly.apply(null, args as never) as never
}

/** `[Experimental]` Parses a TypeScript type annotation as TSchema */
export function ParseOnly<Code extends string, Context extends Record<PropertyKey, TSchema> = {}>(context: Context, code: Code, options?: SchemaOptions): TSchema | undefined
/** `[Experimental]` Parses a TypeScript type annotation as TSchema */
export function ParseOnly<Code extends string>(code: Code, options?: SchemaOptions): TSchema | undefined
/** `[Experimental]` Parses a TypeScript type annotation as TSchema */
export function ParseOnly(...args: any[]): TSchema | undefined {
/** `[Syntax]` Parses a TypeBox TSchema from TypeScript syntax. This function does not infer the type. */
export function ParseOnly<Context extends Record<PropertyKey, Types.TSchema>, Code extends string>(context: Context, code: Code, options?: Types.SchemaOptions): Types.TSchema | undefined
/** `[Syntax]` Parses a TypeBox TSchema from TypeScript syntax */
export function ParseOnly<Code extends string>(code: Code, options?: Types.SchemaOptions): Types.TSchema | undefined
/** `[Syntax]` Parses a TypeBox TSchema from TypeScript syntax. This function does not infer the type. */
export function ParseOnly(...args: any[]): Types.TSchema | undefined {
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 = Module.Parse('Type', code, context)[0] as TSchema | undefined
return (type !== undefined ? CreateType(type, options) : undefined) as never
const type = Module.Parse('Main', code, context)[0] as Types.TSchema | undefined
// Note: Parsing may return either a ModuleInstance or Type. We only apply options on the Type.
return Types.KindGuard.IsSchema(type) ? Types.CloneType(type, options) : type
}
Loading
Loading