Skip to content

String to literal after parse #716

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

You must be logged in to vote

@alexey13 Hi, Try the following

TypeScript Link Here

import { Type, Static } from '@sinclair/typebox'

// ... Base

const BaseComponentSchema = Type.Object({
    type: Type.String(),
    id: Type.String(),
    name: Type.String(),
})

// ... Component

// Create config type which uses generic arguments for each property.
interface CreateComponentSchemaConfig<Type = string, Id = string> {
    id: Id
    type: Type,
}
const CreateComponentSchema = <Type extends string, Id extends string>(config: CreateComponentSchemaConfig<Type, Id>) => {
    return Type.Composite([
        BaseComponentSchema,
        Type.Object({
            id: Type.Literal(config.id),
            type: Type.Literal(config

Replies: 1 comment 1 reply

Comment options

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

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