openfrontio / openfrontio/OpenFrontIO

Remove `enum` types

Open
#966 16 comments 0 reactions 1 assignee Assigned to @JB940 View on GitHub
approved good first issue
Dominant language
TypeScript
Stars
2.7k
Forks
1.4k
Avg merge
17h 43m
Merged PRs (30d)
310

Description

## Description

1. Replace `enum`s with strings unions.

```ts
export enum GameType {
Singleplayer = "Singleplayer",
Public = "Public",
Private = "Private",
}
export const GameTypeSchema = z.nativeEnum(GameType);
```

becomes:
```ts
export const GameTypeSchema = z.union([
z.literal("Private"),
z.literal("Public"),
z.literal("Singleplayer"),
]);
export type GameType = z.infer;
```

2. Block `enum`s with eslint:
```
{
"rules": {
"no-restricted-syntax": [
"error",
// ban all enums
{
"selector": "TSEnumDeclaration",
"message": "See https://www.youtube.com/watch?v=jjMbPt_H3RQ"
}
]
}
}
```

## Justification

Enums do not behave as you might expect at runtime, because enums are not a native feature of javascript. The typescript compiler creates a strange looking object when you use the `enum` keyword:

![Image](https://github.com/user-attachments/assets/7685dd95-14c5-4a1b-ab9c-36636335a066)

And when you run this weird code, you get this exceptional result:

![Image](https://github.com/user-attachments/assets/21e3eafe-977e-487e-81ff-8cd78d525c7b)

Things get a little bit better if you set the enum values to strings, but only if the keys and values match. If they do not match, zod will create types based on the key, but the values will actually be used, resulting in schema validation errors.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.