How to properly implement class constants in TypeScript?
Hi everyone! I'm learning TypeScript and encountered a problem when trying to define constants inside a class.
I need to create a class that will store some fixed values related to this class. For example, I want to define the maximum number of login attempts (MAX_LOGIN_ATTEMPTS = 5) or order statuses (STATUS_PENDING = 'pending', STATUS_APPROVED = 'approved').
I tried different approaches, but none work as I expected:
```typescript
class UserManager {
// Attempt 1: just a property
MAX_LOGIN_ATTEMPTS = 5; // This is an instance property, not a class property
// Attempt 2: static with const
static const MAX_LOGIN_ATTEMPTS = 5; // Syntax error
// Attempt 3: readonly
static readonly MAX_LOGIN_ATTEMPTS = 5; // Works, but can be modifi
No answers yet.
Log in to leave an answer
Blogs