Skip to content
Discussion options

You must be logged in to vote

Hi there! Glad to hear you're enjoying using Pkl 😁

If I'm interpreting your question correctly, you want to add some kind of assertion that the string literal types that are members of the StatusEnum union match a certain pattern. There isn't a super straightforward way to do this, but it is possible with Pkl's reflection library. Here's how:

import "pkl:reflect"

status: StatusEnum = "STATUS_BLEED"

typealias _StatusEnum = "STATUS_BLEED" | "STATUS_COLD" | "STATUS_RAGE" | "STATUS_Frozen"
hidden const statusRegex = Regex(#"^STATUS_([A-Z0-9_]+)$"#)
typealias StatusEnum =
  _StatusEnum(
    (reflect.TypeAlias(_StatusEnum).referent as reflect.UnionType).members.every((it) ->
      let (enumCase

Replies: 1 comment 3 replies

Comment options

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

@HT154
Comment options

@SublunarSage
Comment options

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