• shape_warrior_t@programming.dev
    link
    fedilink
    English
    arrow-up
    1
    ·
    2 days ago

    My preferred way of modelling this would probably be something like
    role: "admin" | "regular" | "logged-out"
    or
    type Role = "admin" | "regular";
    role: Role | null
    depending on whether being logged out is a state on the same level as being a logged-in (non-)admin. In a language like Rust,
    enum Role {Admin, Regular}
    instead of just using strings.

    I wouldn’t consider performance here unless it clearly mattered, certainly not enough to use
    role: number,
    which is just about the least type-safe solution possible. Perhaps
    role: typeof ADMIN | typeof REGULAR | typeof LOGGED_OUT
    with appropriately defined constants might be okay, though.

    Disclaimer: neither a professional programmer nor someone who regularly writes TypeScript as of now.

    • orgrinrt@lemmy.world
      link
      fedilink
      arrow-up
      1
      ·
      edit-2
      1 day ago

      Yeah obviously with constants for the set roles per value. Some languages call them enums, but the point is that what we pass and use is always still the smallest integer type possible. With the extra bonus that if the roles ever become composable, the same value type would likely suffice for a bitflag and only thing needing refactoring would be bitshifting the constants.

      But anyway, this turns out to be the weirdest hill I find myself willing to die on.