-
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?
Conversation
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.
Looks good. Left a few remarks.
export enum TicketStatus { | ||
CLOSED: 'closed', | ||
OPEN: 'open', | ||
WAITING_RESPONSE: 'waiting response', | ||
} |
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.
A bit of a personal preference but for enums I do the following:
export enum TicketStatus { | |
CLOSED: 'closed', | |
OPEN: 'open', | |
WAITING_RESPONSE: 'waiting response', | |
} | |
export enum TicketStatus { | |
Closed = 'Closed', | |
Open = 'Open', | |
WaitingResponse = 'WaitingResponse', | |
} |
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.
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 |
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
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`) |
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.
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 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: |
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.
Newline after.
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.
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: |
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.
Newline after.
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.
it'd -> it would
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.
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: |
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.
Newline after.
// do something | ||
} | ||
``` | ||
or |
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.
Newlines around.
Added a new topic about using
enums
in typescript.