Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(): use enums when needed #91

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions content/typescript/use-enums-when-needed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
title: define enums for variables with already known multiple possible values
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we find a slightly shorter title? Maybe

use enums for named constants

---

# Problem

When we're working with variables, sometimes you'd need to have variables with already known multiple possible values, that will always be the same in your application (think about having a ticket status variable, `CLOSED`, `OPEN`, `WAITING RESPONSE`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Punctuation missing at the end.


# Solution

Typescript allows us to define enumerators, which are variables with multiple defined possible values. By using enumerables TS will be able to infer and autocomplete the possible values of a variable inside your code.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enumerators -> enums
TS -> TypeScript
autocomplete the possible values -> autocomplete possible values


We can create different enumerables as follows:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Newline after.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enumerables -> enums

```ts
export enum TicketStatus {
CLOSED: 'closed',
OPEN: 'open',
WAITING_RESPONSE: 'waiting response',
}
Comment on lines +15 to +19
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit of a personal preference but for enums I do the following:

Suggested change
export enum TicketStatus {
CLOSED: 'closed',
OPEN: 'open',
WAITING_RESPONSE: 'waiting response',
}
export enum TicketStatus {
Closed = 'Closed',
Open = 'Open',
WaitingResponse = 'WaitingResponse',
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also mention to use const enum where possible.

```

We can assign any value to our enumerable properties, in this previous example, if you had numeric values for your variable then it'd look like this:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Newline after.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it'd -> it would

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sentence is a little hard to read. We should break it up a little.

```ts
export enum TicketStatus {
CLOSED: 0,
OPEN:1,
WAITING_RESPONSE: 2,
}
```

So when you or someone else is working later on the code they can use the enum like this:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Newline after.

```ts
if(ticket.status === TicketStatus.OPEN){
// do something
}
```
or
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Newlines around.

```ts
switch(ticket.status){
case TicketStatus.OPEN:
break;
case TicketStatus.CLOSED:
break;
case TicketStatus.WAITING_RESPONSE:
break;
}
```