How to define a schema containing a property belonging to a custom type #1054
Unanswered
leungkinghin-ct
asked this question in
Q&A
Replies: 1 comment 2 replies
-
@leungkinghin-ct Hi, You can use generic types for this. The following creates a generic ResponseSchema that includes the import { Type, TProperties, Static } from '@sinclair/typebox'
/** Generic Type To Create Response Structure */
const ResponseSchema = <Properties extends TProperties>(properties: Properties) => Type.Object({
resultCode: Type.String(),
...properties
})
// A: User
type A = Static<typeof A>
const A = ResponseSchema({
user: Type.Object({
userId: Type.String(),
version: Type.Number(),
createdBy: Type.String(),
// ...
})
})
// B: UserId
type B = Static<typeof B>
const B = ResponseSchema({
userId: Type.String()
}) Hope this helps |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I am new to typebox and have a question. Currently in our application we defined a schema as following
Now we would like to make a change on this schema so it also supports the entire custom type
User
instead of onlyuserId
as followingIn this case, how can we define the
responseSchema
by usingUser
directly instead of extracting every properties fromUser
in schema definition?Thanks
Beta Was this translation helpful? Give feedback.
All reactions