Skip to content

Is there a way to add extra options after a type has been created #697

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

You must be logged in to vote

@bhuynhdev Hi,

TypeBox types just Json Schema objects, so you can just apply additional properties using the JavaScript spread operator. Below applies additional properties from T1 to T2, where we use typeof T1 to give a correct TSchema type of T2 (as they should be the same type)

import { Type as t } from '@sinclair/typebox'

const T1 = t.String()                       // const T1: TString = { type: 'string' }

const T2: typeof T1 = { foo: 'bar', ...T1 } // const T2: TString = { type: 'string', foo: 'bar' }

So, the above applies to all TB types. You may wish to implement a function to make application of additional properties more consistent (where the options are applied before the sche…

Replies: 1 comment 2 replies

Comment options

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

@sinclairzx81
Comment options

Answer selected by bhuynhdev
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