enum & any By albro

View this thread on: d.buzz | hive.blog | peakd.com | ecency.com
·@albro·
0.000 HBD
enum & any By albro
<center>![enum & any](https://images.ecency.com/DQmeMuvnGPcSuZ9Zk6XYjsby1o6BkUZauBL4Wb4jhqnDBjZ/001_understanding_typescript.jpg)</center>

<p>
    One of the concepts that exist in TypeScript is a type of data that has a dual state (like <code>tuples</code>). This type of data is usually used to assign a numerical value that has a string to be read by humans and is called an <code>Enum</code>. <code>Enum</code>s are available in many programming languages, but we don't have access to them in JavaScript.
</p>
<p>
    To write an <code>Enum</code>, you must use the following structure:
</p>
<pre><code class="language-typescript">Enum name {NEW, OLD}</code></pre>
<p>
For today's exercise, I use the following code:
</p>
<pre><code class="language-typescript">const person = {
  name: 'Maximilian',
  age: 30,
  hobbies: ['Sports', 'Cooking'],
  role: [2, 'author']
};</code></pre>
<p>
    I want to change the <code>role</code>. Suppose we have three levels of <code>admin</code>, <code>author </code>and <code>reader </code>(normal users) for the role (user's role on the site). Now we want to have a numerical value for each of these roles to use in the code: <code>admin </code>is <code>0</code>, normal user is <code>1</code>, and <code>author </code>is <code>2</code>.
</p>
<p>
    <i><strong>Question:</strong></i> Why not add numbers normally? For example, in the form of an array?
</p>
<p>
    <i><strong>Answer:</strong></i> Because our possible problems increase. For example, we may add a number that is not defined in the system (for example, level 4 that does not exist). Another example is our own work. If we put the numbers in an array normally, during development we would have to constantly check who each access level was for.
</p>
<p>
    Usually, newbies try to avoid numbers and use strings like <code>Admin </code>or <code>ReadOnly-User</code> to make the code more readable, but the problem is that during coding, for example, in <code>if</code> conditions, they remember that the defined value is <code>Read-only-user</code> or <code>read_only_user</code> or <code>ReadOnlyUser</code> or... That's why constants are usually used in JavaScript, for example:
</p>
<pre><code class="language-typescript">const ADMIN = 'ADMIN';
const USER = 'READ-ONLY-USER';
const AUTHOR = 3;</code></pre>
<p>
    Then these same constants are used instead of writing strings. This method is a suitable method, but it is still possible to enter an invalid value in them by mistake, because, for example, in the code above, <code>AUTHOR</code> has the data type <code>number</code>, so we can put any number in its place!
</p>
<p>
    Now suppose we want to write an <code>enum</code> for this situation:
</p>
<pre><code class="language-typescript">enum Role { ADMIN = 'ADMIN', READ_ONLY = 100, AUTHOR = 'AUTHOR' };</code></pre>
<p>
Usually, in coding conventions, it is said that the <code>enum</code> name must start with a capital letter (<code>Role</code>) and the values ​​inside it must all be written in capital letters (<code>READ_ONLY</code> and...). As you can see, you can use numbers and strings and whatever you want, just like arrays.
</p>
<p>
Also note that we can write the above example as follows:
</p>
<pre><code class="language-typescript">enum Role { ADMIN, READ_ONLY, AUTHOR };</code></pre>
<p>
In this case, <code>index</code> is used as a value, that is, <code>ADMIN </code>is equal to <code>zero</code>, <code>READ_ONLY</code> is equal to <code>one</code>, and <code>AUTHOR</code> is equal to <code>2</code>, but if you want to use a value other than the default indexes, which are numeric, you can use the example above. Use it and give it your own desired values.
</p>
<p>
Also, if you only define a separate number for the first value, the Enum starts there:
</p>
<pre><code class="language-typescript">enum Role { ADMIN = 5, READ_ONLY, AUTHOR };</code></pre>
<p>
Here I have set <code>ADMIN </code>to be <code>5 </code>so <code>READ_ONLY</code> value is <code>6 </code>and <code>AUTHOR</code> value is <code>7</code>. That is, initialization starts from the first number. Note that the name of each of these items (such as <code>ADMIN</code>) is a type of label, which means we only use it to work in the code. For example, in the above code, <code>ADMIN </code>is equal to <code>5</code>, so wherever we use <code>Role.ADMIN</code>, the number <code>5 </code>is given to us, not the string <code>ADMIN</code>. This can cause you to make mistakes in string mode.
</p>
<p>
We can use it as an example to say:
</p>
<pre><code class="language-typescript">enum Role { ADMIN, READ_ONLY, AUTHOR };
const person = {
  name: 'Maximilian',
  age: 30,
  hobbies: ['Sports', 'Cooking'],
  role: Role.ADMIN
};</code></pre>
<p>
    Then, to try the codes, you can say:
</p>
<pre><code class="language-typescript">if (person.role === Role.AUTHOR) {
  console.log('is author');
}</code></pre>
<p>
    Since above we had set the value of <code>Role.ADMIN</code> for the <code>role</code>, the <code>console.log</code> statement should not be executed and if we go to the browser, I will not see anything. Why? Because <code>ADMIN </code>is not the same as <code>AUTHOR </code>and the condition is wrong.
</p>
<p>
    In addition, as I said, Enums are only in Typescript and do not exist externally in JavaScript, so if you run the <code>tsc app.ts</code> command and go to the <code>app.js</code> file, you will easily notice the differences:
</p>
<pre><code class="language-typescript">let Role;
(function (Role) {
    Role["ADMIN"] = "ADMIN";
    Role[Role["READ_ONLY"] = 100] = "READ_ONLY";
    Role["AUTHOR"] = "AUTHOR";
})(Role || (Role = {}));
;
var person = {
    name: 'Maximilian',
    age: 30,
    hobbies: ['Sports', 'Cooking'],
    role: Role.ADMIN
};</code></pre>
<p>
    As you can see, implementing our simple code in JavaScript is complicated and time-consuming, and this is one of the beauties of the TypeScript language, which makes our work much easier.
</p>
<h3>
    <code>any</code> type in typescript
</h3>
<p>
    <code>any</code> means "anything", so if you give this type to any variable, it means that you have allowed any value to be placed in it. Pay attention to the following example:
</p>
<pre><code class="language-typescript">let favoriteActivities: any;</code></pre>
<p>
The <code>favoriteActivities </code>variable can take any value and there will be no problem. Using <code>any</code> is not recommended because it takes away the benefits of TypeScript. At least we can say instead of <code>any</code>:
</p>
<pre><code class="language-typescript">let favoriteActivities: any[];</code></pre>
<p>
In this case, at least we say that it is an array that accepts any value in itself so that we are a little more limited. <code>any </code>has no special explanation and is really that simple. Note that most programmers recommend that you avoid <code>any </code>as much as possible unless you don't know what value one of your variables is going to take. In such a situation, you can temporarily use <code>any </code>to avoid making a mistake and then come back in the future and choose the correct type for it.
</p>
👍 , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,