Opinion on DX of this code, is it too confusing or easy to grasp?

const roles = makeEnum("User", "Admin", "Owner"); // Type: "User", "Admin", "Owner"
export type Roles = ExtractEnumType<typeof roles>; // "User" | "Admin" | "Owner"
export const Roles = TrimEnum(roles);


const [IRoles, iroles] = makeEnum("User", "Admin", "Owner"); // Type: "User", "Admin", "Owner"
export type IRoles = ExtractEnumType<typeof iroles>; // "User" | "Admin" | "Owner"
export{ IRoles };
const roles = makeEnum("User", "Admin", "Owner"); // Type: "User", "Admin", "Owner"
export type Roles = ExtractEnumType<typeof roles>; // "User" | "Admin" | "Owner"
export const Roles = TrimEnum(roles);


const [IRoles, iroles] = makeEnum("User", "Admin", "Owner"); // Type: "User", "Admin", "Owner"
export type IRoles = ExtractEnumType<typeof iroles>; // "User" | "Admin" | "Owner"
export{ IRoles };
Just wanted to get some second opinions on a fun library I'm working on. I'm reaching a point where typescript limitations are making me have to do strange patterns to get my desired behavior, so I want to try to pick a variation of it that resonates with other people and not just myself. As of the moment these are the only 2 that I could imagine, but if anyone has an idea on another way that I could return a type value/object and export them so that they appear as the same type. Additionally, is it safe to say that the top version is the more 'normal' version and the bottom one is definitely more react-like because of the array destrucuring or is that something common in all of javascript?
2 Replies
Nate
Nate16mo ago
i’m not sure where makeEnum() comes from, so it might be better, but I’ve been using zod’s enum like const roles = z.enum([“User”, “Admin”, “Owner”])
MagerX
MagerX16mo ago
its a library I've been working on that I put on npm earlier today. I wasn't aware zod even had enums but they look interesting, though a bit more verbose than I would prefer. My library the set up is slightly more verbose but the trade off is more concise usage eg.
function Test(role: IRoles) {
console.log(role);
}

Test(IRoles.Admin);
Test(IRoles.User);
Test(IRoles.Owner);
function Test(role: IRoles) {
console.log(role);
}

Test(IRoles.Admin);
Test(IRoles.User);
Test(IRoles.Owner);
Want results from more Discord servers?
Add your server