Skip to content

Extending TypeBuilder and the compiler #263

Answered by sinclairzx81
giggo1604 asked this question in Q&A
Discussion options

You must be logged in to vote

@giggo1604 Hi,

Just an update, TypeBox now provides the ability to extend the compiler and value API's with user defined types. Documentation on this feature can be found here.

The following example shows creating a user defined Date type.

TypeScript Link Here

import { TypeCompiler } from '@sinclair/typebox/compiler'
import { Type, Kind } from '@sinclair/typebox'
import { Custom } from '@sinclair/typebox/custom'

Custom.Set('Date', (schema, value) => value instanceof Date) // custom type check

const T = Type.Unsafe<Date>({ [Kind]: 'Date' })              // custom type schema

const R = TypeCompiler.Compile(T).Check(new Date())          // true

As this functionality has landed a little ea…

Replies: 2 comments 7 replies

Comment options

You must be logged in to vote
5 replies
@giggo1604
Comment options

@sinclairzx81
Comment options

@giggo1604
Comment options

@sinclairzx81
Comment options

@sinclairzx81
Comment options

Comment options

You must be logged in to vote
2 replies
@giggo1604
Comment options

@sinclairzx81
Comment options

Answer selected by sinclairzx81
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants