npx agentspec-cli install 4226b20d-f617-4747-9f8e-43d42f829749 --file-type cursor-rules→ .cursor/rules/
---
description: When creating an enum in TypeScript
globs:
alwaysApply: false
---
Do not introduce new enums into the codebase. Retain existing enums.
If you require enum-like behaviour, use an `as const` object:
```ts
const backendToFrontendEnum = {
xs: "EXTRA_SMALL",
sm: "SMALL",
md: "MEDIUM",
} as const;
type LowerCaseEnum = keyof typeof backendToFrontendEnum; // "xs" | "sm" | "md"
type UpperCaseEnum = (typeof backendToFrontendEnum)[LowerCaseEnum]; // "EXTRA_SMALL" | "SMALL" | "MEDIUM"
```
Remember that numeric enums behave differently to string enums. Numeric enums produce a reverse mapping:
```ts
enum Direction {
Up,
Down,
Left,
Right,
}
const direction = Direction.Up; // 0
const directionName = Direction[0]; // "Up"
```
This means that the enum `Direction` above will have eight keys instead of four.
```ts
enum Direction {
Up,
Down,
Left,
Right,
}
Object.keys(Direction).length; // 8
```
TypeScript Best Practices
Cursor IDE rules and conventions for AI-assisted development.
by AgentHub Crawler
TypeScript Best Practices
Cursor IDE rules and conventions for AI-assisted development.
by AgentHub Crawler
TypeScript Best Practices
Cursor IDE rules and conventions for AI-assisted development.
by AgentHub Crawler
syntax-and-formatting
Cursor IDE rules and conventions for AI-assisted development.
Sign in to leave a comment.
by AgentHub Crawler