0

I am just a beginner in Typescript and I am going over a practice assessment question about Enums in Typescript. I am totally confused with this question:

Extend the Permission enum so that it has the following fields:

Read, which should have a value of 1

Write, which should have a value of 2

Execute, which should have a value of 4

Extend the getPermissionList function so that it accepts a combination of several Permission enums and returns an array of strings which are the names of the Permissions.

For example, executing:

getPermissionList(Permission.Read | Permission.Write); should return [ 'Read', 'Write' ] in any order.

So far I have something like this but I don't know if am in the ballpark:

function getPermissionList(permission: Permission): string[] {
     let arr: string[]
     for (let enumMember in permission) {
        arr.push(enumMember);
    }
    return arr;
}

enum Permission {
    Read = 1,
    Write = 2,
    Execute = 4,
    
}

//console.log(getPermissionList(Permission.Read | Permission.Write));

I know it isn't right as I am getting errors but I feel like im banging my head against a wall right now.

5
  • Does this answer your question? Pass / Get multiple Enum values in Typescript? Commented Jan 30, 2022 at 9:50
  • It doesn't solve it because that one just takes the full Enum and filters it in, if you look at the question they asked me, it is a function that takes two parameters: getPermissionList(Permission.Read | Permission.Write); should return [ 'Read', 'Write' ] in any order. Commented Jan 30, 2022 at 9:58
  • Permission.Read | Permission.Write is not two parameters. It's a binary OR of two enum values, as in the linked question. Please check that one and compare with your case; the only major difference I can see is that enum keys are not extracted there. Commented Jan 30, 2022 at 10:00
  • I know it is the OR, however the expected solution to having the OR in the function produces a result of " should return [ 'Read', 'Write' ]". While it is the OR, it still produces a result with an array with BOTH Read and Write Commented Jan 30, 2022 at 10:03
  • Are you a beginner in JavaScript as well or just TypeScript? If you know JavaScript, then Permission is just an object that looks like {Read: 1, Write: 2, Execute: 4, "1": "Read", "2": "Write", "4": "Execute"}. So you will need to do bitwise arithmetic on permission and then use the set bits to pull keys or values out of the Permission object. You could maybe do it this way but there's lots of ways to approach it. Commented Jan 30, 2022 at 16:52

1 Answer 1

1

This involves taking advantage of bitwise arithmetic for enums. You can learn more about them here https://www.typescriptlang.org/docs/handbook/enums.html. For this question, it seems like your input for your enum values are only numbers, so we can use the bitwise & operator to compare permission with the possible enum types for Permission. The following is a brittle solution showcasing this concept:

function getPermissionList(permission: Permission): string[] {

  let arr: string[] = []
  if(permission & Permission.Read) {
    arr.push( Permission[Permission.Read] )
  }

  if(permission & Permission.Write) {
    arr.push( Permission[Permission.Write] )
  }

  if(permission & Permission.Execute) {
    arr.push( Permission[Permission.Execute] )
  }

  return arr;

}

Another approach that loops through an enum's names and does the above may look like this:

function getPermissionList(permission: Permission): string[] {
  let arr: string[] = []
  for (var key  in Permission) {
    if (isNaN(Number(key)) ) { //skips over value : key definitions which assumes values are numbers
       if( permission & Permission[key as keyof typeof Permission] ) {
         let enumName = Permission[ Permission[key as keyof typeof Permission] ]
         arr.push( enumName )
       } 
    }    
  }
  return arr;
}

I hope this helps and as an exercise, I would recommend figuring out how to solve this problem with string enums :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.