resolution of Literal Types.Union from const #315
-
I've my const such as
need to convert it into
is it possible with generic helpers provided by TypeBox ? |
Beta Was this translation helpful? Give feedback.
Answered by
sinclairzx81
Jan 30, 2023
Replies: 1 comment 3 replies
-
@ahhmarr Hi, you can try something like the following 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 |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
ahhmarr
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@ahhmarr Hi, you can try something like the following
TypeScript Link Here
Hope this helps
S