Skip to content

[Suggestion] Use string unions instead of enums #341

@ayush-seth

Description

@ayush-seth

Feature request

Overview

Use of enums is generally discouraged in typescript because they don't exist natively in javascript and have some weird runtime behaviour. You can find many videos on youtube explaining why we should avoid enums. Source 1 Source 2

This library uses enums to define options we can pass into directives, for example side in rdxHoverCardContent.

My real gripe with using enums here is not even any of the reasons outlined in the videos, we can even ignore them for now but the main pain point here is that just to pass an option to side I have to first import the enum, declare a variable in the class and then reference that variable in the template just so I can configure the side.

So instead of being able to do

<ng-template
          rdxHoverCardContent
          [side]="bottom"
          [sideOffset]="8"
        >

I have to do

import { RdxPositionSide } from "@radix-ng/primitives/core";
...
<ng-template
          rdxHoverCardContent
          [side]="HoverCardSide.Bottom"
          [sideOffset]="8"
        >
...
export class Component {
  protected readonly HoverCardSide = RdxPositionSide;
}

If instead, side was typed as "top" | "bottom", I would still get all the type-safety I get from enums but I would also get better dx when using the library.

Other libraries

radix-ui follows the same.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions