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

Conversation

eduardoRoth
Copy link
Contributor

Added a new topic about using enums in typescript.

Copy link
Member

@d3lm d3lm left a comment

Choose a reason for hiding this comment

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

Looks good. Left a few remarks.

Comment on lines +15 to +19
export enum TicketStatus {
CLOSED: 'closed',
OPEN: 'open',
WAITING_RESPONSE: '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.

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.

@@ -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


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.

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

}
```

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.

}
```

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.

// 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants