1
1
/*--------------------------------------------------------------------------
2
2
3
- @sinclair /typebox
3
+ @sinclair /typebox/syntax
4
4
5
5
The MIT License (MIT)
6
6
@@ -26,29 +26,35 @@ THE SOFTWARE.
26
26
27
27
---------------------------------------------------------------------------*/
28
28
29
+ import * as Types from '@sinclair/typebox'
29
30
import { Static } from '@sinclair/parsebox'
30
- import { CreateType } from '@sinclair/typebox'
31
- import { TSchema , SchemaOptions } from '@sinclair/typebox'
32
31
import { Module } from './runtime'
33
- import { Type } from './static'
32
+ import { Main } from './static'
34
33
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. */
40
45
export function Parse ( ...args : any [ ] ) : never {
41
46
return ParseOnly . apply ( null , args as never ) as never
42
47
}
43
48
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 {
50
55
const withContext = typeof args [ 0 ] === 'string' ? false : true
51
56
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
54
60
}
0 commit comments