0

We use enums for hooks to ingest in our messages files. It looks like this:

import { defineMessages } from 'react-intl';

export enum SettingsMessages {
  Settings = 'app.views.settings.title',
}

export default defineMessages({
  settings: {
    id: 'app.views.settings.title',
    defaultMessage: 'Settings',
  },
});

It works well but I'd like to enforce the following things with typescript:

  1. The messages object is exactly that structure. No more, no less
  2. There is a 1:1 relationship between the enum values and the id keys in the object. I'd like TypeScript to throw an error if the enum value is not located in the object and vice versa. Also it'd be nice if it threw an error if there is a message id that is not in the enum.

Essentially since there are two sources of truth for this, I'd like to make it a bit safer against typos and forgetting to add it as these files get pretty large. Removing the enums is out of the question

Any help is appreciated

Bobby Gagnon
  • 80
  • 2
  • 11

0 Answers0