Kunok
Kunok

Reputation: 8759

How do I force object to include all enum values as property keys?

Lets assume I have this enum:

export enum translationKeys {
  resp_action_denied = "resp_action_denied",
  resp_invalid_request = "resp_invalid_request",
}

I use this enum to extract unique string values from single source. Each of them maps to string resolution object:

const translations: {
  [key: string]: RequiredTranslations;
} = {
  resp_action_denied: {
    ENG: "Foo bar",
    GER: "Ich bin"
  },
  resp_invalid_request: {
    ENG: "Baz",
    GER: "Das"
  },

Is there any way to force this object to include all enums as keys otherwise throw compile error?

Upvotes: 3

Views: 1028

Answers (2)

Doğancan Arabacı
Doğancan Arabacı

Reputation: 3982

If you'd like, you can also use const assertions and get rid of enums:

export const translationKeys = {
  resp_action_denied: 'resp_action_denied',
  resp_invalid_request: 'resp_invalid_request',
}
const languages = ['ENG', 'GER'] as const
type RequiredTranslations = { [key in (typeof languages)[number]]: string }

type Translations = {
  [key in keyof typeof translationKeys]: RequiredTranslations
}
const translations: Translations = {
  resp_action_denied: {
    ENG: 'Foo bar',
    GER: 'Ich bin',
  },
  resp_invalid_request: {
    ENG: 'Foo bar',
    GER: 'Ich bin',
  },
}

P.S: you'll need typescript 3.4 and greater

Upvotes: 0

Titian Cernicova-Dragomir
Titian Cernicova-Dragomir

Reputation: 249606

You can just use the mapped type Record:

export enum translationKeys {
  resp_action_denied = "resp_action_denied",
  resp_invalid_request = "resp_invalid_request",
}

const translations: Record<translationKeys, { ENG: string, GER: string}> = {
  resp_action_denied: {
    ENG: "Foo bar",
    GER: "Ich bin"
  },
  [translationKeys.resp_invalid_request]: { // can also use computed prop to reference the enum instead of the value. 
    ENG: "Baz",
    GER: "Das"
  }
};

Upvotes: 2

Related Questions