Skip to content

resolution of Literal Types.Union from const #315

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

You must be logged in to vote

@ahhmarr Hi, you can try something like the following

TypeScript Link Here

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

export function UnionOfString<T extends string[]>(values: readonly [...T], options: SchemaOptions = {}) {
  return Type.Union(values.map(value => Type.Literal(value)), options)
}

const Allowed = ["chips", "banana", "apple"] as const 

const T = UnionOfString(Allowed)

type T = Static<typeof T> // type T = "chips" | "banana" | "apple"

Hope this helps
S

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@thes01
Comment options

@sinclairzx81
Comment options

@thes01
Comment options

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