3

Is it possible to type the enum array in a way that forces it to contain every single value of the EPostFromField enum?

This is a mongodb schema, and the use case would be to future proof the enum field if more enums are added later on (ie: have the compiler throw an error because the array doesn't have all the enum values enumerated).

As a bonus I guess the next level would be a solution that also guarantees that the enum array values are unique :)

export const enum EPostFromField {
  Resident = 'resident',
  AdminUser = 'admin-user', // Admin <user name>
  BoardUser = 'board-user', // Board <user name>
  AdminSociety = 'admin-society', // Admin <community name>
  BoardSociety = 'board-society', // Board <community name>
}

showPostAs: {
    type: String,
    default: EPostFromField.Resident,
    enum: [
      EPostFromField.Resident,
      EPostFromField.AdminUser,
      EPostFromField.BoardUser,
      EPostFromField.AdminSociety,
      EPostFromField.BoardSociety,
    ] as EPostFromField[], // DEVNOTE: Improve typing to enforce *every* unique key of enum
  },
3
  • Possible duplicate of Enforce that an array is exhaustive over a union type Commented Jun 3, 2020 at 0:28
  • 1
    enum objects, like all objects, have keys and values which are different. "Resident" is a key, but EPostFromFileld.Resident is its value. You talk about wanting to guarantee unique keys, but you are making an array of values. Are you using the word "key" when you mean "value"? If not, I'm confused; could you elaborate? Commented Jun 3, 2020 at 0:31
  • @jcalz Yes. Sorry for mixup. The end goal is to have the mongodb enum field contain (and therefore only accept) values of enum EPostFromField. Thank you and I will edit the question. Commented Jun 3, 2020 at 15:41

1 Answer 1

5

You could define a string enum and do the following:

enum EPostFromField {
    Resident = "Resident",
    AdminUser = "AdminUser",
    BoardUser = "BoardUser",
    AdminSociety = "AdminSociety",
    BoardSociety = "BoardSociety"
}

const epostFromFieldKeys = Object.keys(EPostFromField);

const epostFromFields = epostFromFieldKeys as EPostFromField[]

You can see this running in this playground link.

The values in the array would be unique if you define the string values in the enum uniquely(!).

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

2 Comments

Thanks Ben! Yes, in effect this answers my question :) I was however searching for something a bit more condensed. Take the typescript Partial<T> utility type, I was hoping for something more along these lines (but instead of a partial match, a complete + mandatory + unique match).
Not work for const enum

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.