Single-use variables: can you ensure each enum member is only referenced once?

Viewed 49

I'm working on a React app written in Typescript.

I want to give each of 100+ components a unique ID that remains constant over user sessions:

<Component id={"ID_1"} />
<Component id={"ID_2"} />

No two components should ever be assigned the same ID.

At the moment, I'm using a TypeScript enum to keep track, but there's nothing stopping me, or a colleague, from accidentally assigning the same enum member to two components:

enum Ids = {
  Id1,
  Id2,
}

<Component id={Ids.Id1}/>
<Component id={Ids.Id1}/> // oops

Does anyone know of a way to ensure that an enum member (or other variable) is only referenced once, across an entire codebase, at compile time?

I've wondered if this can be achieved using an ESLint rule but can't find any examples of ESLint counting references across an entire codebase? It also feels like this could get really slow with the linter running live as you type.

0 Answers
Related