-
Notifications
You must be signed in to change notification settings - Fork 66
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||||||||||||||||||||||
--- | ||||||||||||||||||||||
|
||||||||||||||||||||||
# 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`) | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. enumerators -> enums |
||||||||||||||||||||||
|
||||||||||||||||||||||
We can create different enumerables as follows: | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Newline after. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would also mention to use |
||||||||||||||||||||||
``` | ||||||||||||||||||||||
|
||||||||||||||||||||||
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: | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Newline after. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it'd -> it would There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Newline after. |
||||||||||||||||||||||
```ts | ||||||||||||||||||||||
if(ticket.status === TicketStatus.OPEN){ | ||||||||||||||||||||||
// do something | ||||||||||||||||||||||
} | ||||||||||||||||||||||
``` | ||||||||||||||||||||||
or | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
``` |
There was a problem hiding this comment.
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