Skip to content

How to create typebox type that is the same as typescript typeof MyEnum? #509

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

You must be logged in to vote

@meightythree Hi,

You can pass a enum into Type.Enum which will create a union (anyOf) schema. You can then use Static<typeof T> which will return the original enum type (as it would be understood by TypeScript)

enum MyEnum {
   first = 'first',
   second = 'second',
}

const T = Type.Enum(MyEnum)    // const T = {
                               //   anyOf: [{
                               //       type: 'string',
                               //       const: 'first',
                               //   }, {
                               //       type: 'string',
                               //       const: 'second',
                               //     }
                               

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@meightythree
Comment options

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