<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Evans Ibok</title>
    <description>The latest articles on DEV Community by Evans Ibok (@evansibok).</description>
    <link>https://dev.to/evansibok</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F157501%2F46577255-5dc0-4019-9c3b-6724c44f58ce.png</url>
      <title>DEV Community: Evans Ibok</title>
      <link>https://dev.to/evansibok</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/evansibok"/>
    <language>en</language>
    <item>
      <title>Bootstrapping a React Native Typescript App</title>
      <dc:creator>Evans Ibok</dc:creator>
      <pubDate>Fri, 19 Mar 2021 12:50:35 +0000</pubDate>
      <link>https://dev.to/evansibok/bootstrapping-a-react-native-typescript-app-1l24</link>
      <guid>https://dev.to/evansibok/bootstrapping-a-react-native-typescript-app-1l24</guid>
      <description>&lt;p&gt;&lt;strong&gt;Cover Image Credits&lt;/strong&gt;: Photo by &lt;em&gt;&lt;a href="https://unsplash.com/@rsdiz" rel="noopener noreferrer"&gt;Muhammad Rosyid Izzulkhaq&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;For Javascript developers out there, I'm certain we've all heard about the popular kids on the block. I'm talking about React Native (A Cross-platform mobile app development library) and Typescript (A Javascript superset with static typing).&lt;/p&gt;

&lt;p&gt;If you haven't heard about Typescript, here's a blog post that serves as an &lt;a href="https://blog.evansibok.com/introduction-to-typescript-and-its-basic-types" rel="noopener noreferrer"&gt;Introduction to Typescript and its Basic Types&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Prerequisite
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;A basic understanding of Javascript and React&lt;/li&gt;
&lt;li&gt;Has already setup development environment for React Native, otherwise, refer to the &lt;a href="https://reactnative.dev/docs/environment-setup" rel="noopener noreferrer"&gt;React Native Environment Setup Guide&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In this post, we will begin our &lt;a href="https://blog.evansibok.com/series/truth" rel="noopener noreferrer"&gt;building-in-public&lt;/a&gt; series, where we will learn how to initialize a new React Native project with Typescript support.&lt;/p&gt;

&lt;p&gt;We will focus on initializing a sample application with all its project files.&lt;/p&gt;

&lt;p&gt;Without further ado, let's get started!&lt;/p&gt;

&lt;h2&gt;
  
  
  Initialize a new React Native App
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Cd into a folder where you want to create your application.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For me, that folder is &lt;code&gt;projects&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When you're inside the folder, run the command below to create a new react native application with typescript support:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;react-native init truth --template react-native-template-typescript
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The command above uses the official &lt;code&gt;react-native-cli&lt;/code&gt; to create a new application called &lt;code&gt;truth&lt;/code&gt; and installs a template with typescript support. You must have the &lt;code&gt;react-native-cli&lt;/code&gt; package installed globally on your machine for the code above to work.&lt;/p&gt;

&lt;p&gt;If you don't want to install the &lt;code&gt;react-native-cli&lt;/code&gt; global package you can run the code below to get the same results. Like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx react-native init truth --template react-native-template-typescript
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Cd into the app project directory.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd truth
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code above enables us to navigate into the newly created app folder.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Open the application in your code editor of choice.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The application folder structure should look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fepxdgsh4pxk6sm1o69fr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fepxdgsh4pxk6sm1o69fr.png" alt="rn-folder-structure"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Finalize configuration
&lt;/h2&gt;

&lt;p&gt;If you look at the project structure image above, you will see that we have a &lt;code&gt;tsconfig.json&lt;/code&gt; file. This is the file where our typescript configurations live in.&lt;/p&gt;

&lt;p&gt;Before we proceed to run our app, we will have to add custom path mappings for our project to make it easier for folder access and navigation.&lt;/p&gt;

&lt;p&gt;To do this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Open the &lt;code&gt;tsconfig.json&lt;/code&gt; file, find the lines of code that look like these that have been commented out&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"baseUrl": './',
"paths": {},
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Uncomment the &lt;code&gt;baseUrl&lt;/code&gt; line, and replace the &lt;code&gt;paths&lt;/code&gt; line with the following code&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"paths": {
  "*": ["src/*"],
  "tests": ["tests/*"],
  "@components/*": ["src/components/*"],
},
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What we're doing here is setting the base URL path to be the project root folder and adding paths that will automatically resolve to the ones we added in the code above.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;*&lt;/code&gt; path sets the base path for the &lt;code&gt;src&lt;/code&gt; folder which we will create later. The &lt;code&gt;src&lt;/code&gt; folder is where all our code files for the application will live in.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;tests&lt;/code&gt; path tells typescript to read every file that lives in the &lt;code&gt;tests&lt;/code&gt; folder. Useful when running tests.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;@components/*&lt;/code&gt; path is to read every file in the components folder. We will create this folder in the future as well.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Add &lt;code&gt;babel-plugin-module-resolver&lt;/code&gt; to our project dependency to resolve our custom path mappings.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Run the code below in the terminal of your application root folder&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install -D babel-plugin-module-resolver
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This should add the &lt;code&gt;babel-plugin-module-resolver&lt;/code&gt; to your package.json development dependencies.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Locate and update the &lt;code&gt;babel.config.js&lt;/code&gt; file&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When you open the&lt;code&gt;babel.config.js&lt;/code&gt; file in your project root directory, you'll find a file that looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add the code below right after the line with the &lt;code&gt;presets&lt;/code&gt; configuration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;plugins: [
  [
    'module-resolver',
    {
      root: ['./src'],
      extensions: ['.ios.js', '.android.js', '.js', '.ts', '.tsx', '.json'],
      alias: {
        tests: ['./tests/'],
        '@components': './src/components',
      },
    },
  ],
],
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This configuration makes it possible for babel to resolve the typescript configurations we added earlier.&lt;/p&gt;

&lt;p&gt;Now we can proceed to run our application and try it out.&lt;/p&gt;

&lt;h2&gt;
  
  
  Running our Application (Android)
&lt;/h2&gt;

&lt;p&gt;To run our newly created application, we will use an android emulator available to us from &lt;code&gt;Android Studio&lt;/code&gt; to test the android version of our app.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1
&lt;/h3&gt;

&lt;p&gt;Startup your emulator of choice. Make sure you have a virtual device running.&lt;/p&gt;

&lt;p&gt;If you're using &lt;code&gt;Android Studio&lt;/code&gt;, you can check this by typing the following code in your terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;adb devices
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will show you a list of devices available to use to run your app.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2
&lt;/h3&gt;

&lt;p&gt;Open the &lt;code&gt;package.json&lt;/code&gt; file in your project root directory and observe the &lt;code&gt;'scripts'&lt;/code&gt; available.&lt;/p&gt;

&lt;p&gt;To start our application, run the code below in your terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run android
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will run the &lt;code&gt;"android"&lt;/code&gt; command in the &lt;code&gt;"scripts"&lt;/code&gt; section of your package.json file which will run and install the application on our virtual device.&lt;/p&gt;

&lt;p&gt;After the application has finished installing on the virtual device. You should see the demo application open on the virtual device that looks like the image below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2kq8jifu5uybdpo9jcb2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2kq8jifu5uybdpo9jcb2.png" alt="truth-emulator"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Resources&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If you're not familiar with Android Studio, learn more about it on the &lt;a href="https://developer.android.com/studio" rel="noopener noreferrer"&gt;Official Android Studio Documentation&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;For a more in-depth approach to running a React Native application, refer to the &lt;a href="https://reactnative.dev/docs/running-on-device" rel="noopener noreferrer"&gt;Official React Native Documentation&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Voila! 🎉🎉🎉&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You just created and set up a React Native application with Typescript support from scratch.&lt;/p&gt;

&lt;p&gt;Next, we will setup Authentication/Authorization routes for our application using React Navigation.&lt;/p&gt;

&lt;p&gt;Thank you and see you in the next post.&lt;/p&gt;

</description>
      <category>reactnative</category>
      <category>typescript</category>
      <category>javascript</category>
      <category>mobile</category>
    </item>
    <item>
      <title>Introduction to Typescript and its Basic Types</title>
      <dc:creator>Evans Ibok</dc:creator>
      <pubDate>Fri, 19 Mar 2021 11:26:16 +0000</pubDate>
      <link>https://dev.to/evansibok/introduction-to-typescript-and-its-basic-types-47i0</link>
      <guid>https://dev.to/evansibok/introduction-to-typescript-and-its-basic-types-47i0</guid>
      <description>&lt;p&gt;&lt;strong&gt;Cover Image Credits&lt;/strong&gt;: Photo by &lt;em&gt;&lt;a href="https://unsplash.com/@lucabravo"&gt;Luca Bravo&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Typescript has become quite the popular topic of discussion among developers and companies in the tech space. So, what exactly is this Typescript everyone's talking about? This article will attempt to explain Typescript in the simplest form possible and also introduce you to the basic types available. &lt;/p&gt;

&lt;h2&gt;
  
  
  What is Typescript?
&lt;/h2&gt;

&lt;p&gt;Typescript is commonly known as a Superset of Javascript. What does this mean? Typescript is a programming language built on top of Javascript. It introduces new features and advancements to the Javascript programming language. It offers the ability to use newer, more modern features when writing your Javascript code.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Typescript can not be run in a Javascript environment like the browser.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  How then does Typescript work?
&lt;/h2&gt;

&lt;p&gt;In addition to being a programming language, Typescript is also a compiler. It converts all Typescript codes to Javascript code so it can be run by environments that support Javascript, like your browser.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why use Typescript?
&lt;/h2&gt;

&lt;p&gt;Typescript in its simplest form is just &lt;strong&gt;&lt;em&gt;typed&lt;/em&gt;&lt;/strong&gt; javascript.&lt;/p&gt;

&lt;p&gt;Take the below code for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function addTwoNumbers(num1, num2) {
    return num1 + num2;
};

addTwoNumbers(5, 6);
addTwoNumbers('5', '6');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Javascript, the function above will run and output without errors. In the first function call, our output will be &lt;code&gt;11&lt;/code&gt;, while in the second function call, the output will be the string &lt;code&gt;56&lt;/code&gt;. As far as Javascript is concerned, we don't have any errors, but as you can see, our function is meant to add two numbers and not two strings. So in this case, our code fails the test silently by not throwing an error when either of &lt;code&gt;num1&lt;/code&gt; and &lt;code&gt;num2&lt;/code&gt; parameters are strings. These and other hidden issues expose the weaknesses we have in the Javascript programming language.&lt;/p&gt;

&lt;p&gt;Typescript aims to solve this, in addition to adding other superpowers to Javascript.&lt;/p&gt;

&lt;h2&gt;
  
  
  Typescript Basics
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Core Types and how to define them&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;em&gt;Number&lt;/em&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;8, -4, 2.8
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Typescript regards all digits as a &lt;code&gt;number&lt;/code&gt; type. Including single digits, negative integers, and floating-point numbers.&lt;/p&gt;

&lt;p&gt;To define a &lt;code&gt;number&lt;/code&gt; type in typescript we can do the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Option 1
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let someNumber: number;

someNumber = 10;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In option 1 above, we are explicitly stating the type of the variable, in this case: &lt;code&gt;someNumber&lt;/code&gt;. This option works when we don't want to assign a value on the spot. In this case, whatever value we assign to the variable in the future, typescript will treat it as a &lt;code&gt;number&lt;/code&gt; type because we told typescript to do so. If we do this:&lt;br&gt;
&lt;code&gt;someNumber = 'my name';&lt;/code&gt;, &lt;br&gt;
typescript would throw an error as we are attempting to assign a &lt;code&gt;string&lt;/code&gt; value to a variable that has a &lt;code&gt;number&lt;/code&gt; type definition.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Option 2
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let someNumber: number = 5;

someNumber = 10;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Option 2 works when we are assigning a value on the spot and we still want to explicitly state the type. We can still change the value of the variable &lt;code&gt;someNumber&lt;/code&gt; in the future.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Option 3
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let someNumber = 5;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Option 3 is a bit different because we aren't explicitly stating the type. We just define the variable &lt;code&gt;someNumber&lt;/code&gt; and assign a value to it which is &lt;code&gt;5&lt;/code&gt;. In this situation, typescript does something called &lt;code&gt;Type Inference&lt;/code&gt;, where it uses the value assigned to infer the type that should be assigned to the variable. This is the same in all type definitions.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;em&gt;String&lt;/em&gt;
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'my word', "my word", `my word`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;All text values are considered strings, whether it's single quotes, double quotes, or the newer string literals.&lt;/p&gt;

&lt;p&gt;For string types, we can define them as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Option 1
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let someString: string;

someString = "a string value";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Option 2
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let someString: string = 'first string';

someString = 'new string value';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Option 3
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let name: string = 'peter';

let someString = `this is some string ${name}`;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We can see the exact same method of definition here like we did with the number type definition. We can either explicitly define the string variable type or let typescript infer it. We can use single quotes, double quotes or string literals as well.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;em&gt;Boolean&lt;/em&gt;
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;true, false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;A boolean has either a &lt;code&gt;true (1)&lt;/code&gt; or a &lt;code&gt;false (0)&lt;/code&gt; value.&lt;/p&gt;

&lt;p&gt;Boolean types can be defined as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Option 1
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let isTruthy: boolean;

isTruthy = false;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Option 2
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let isTruthy: boolean = true;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Option 3
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let isTruthy = true;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  &lt;em&gt;Object&lt;/em&gt;
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  name: 'Andy',
  age: 23,
  isEmployed: true
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Objects look exactly like their vanilla javascript counterparts.&lt;/p&gt;

&lt;p&gt;We can define objects in the following ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Option 1
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let firstObject: {
  color: string;
  age: number;
};

firstObject = {
  color: 'blue',
  age: 30
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;By paying close attention to option 1 above, you can notice this method of definition is a bit different. We define the object with its properties and explicitly state the types for each property. If we try to assign a different value type to the properties, we will definitely get an error.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Option 2
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let secondObject: {
  color: string;
  age: number;
} = {
  color: 'red',
  age: 25
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The definition in option 2 above becomes useful when we want to assign a value on the spot. It still makes use of the object type definition with other types as properties.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Option 3
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const thirdObject = {
  color: 'red',
  age: 25
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;For this definition, we are again just defining our object with properties and letting typescript infer the types based on the value we assign to the properties. I'm sure you're getting how this &lt;em&gt;type definition&lt;/em&gt; thing works now.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Other valid but not recommended ways of defining object type&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;While there are situations where the methods below can be used, I wouldn't recommend using them, but if you have to, they should be used sparingly. The methods described above are my most recommended methods for &lt;code&gt;object&lt;/code&gt; type definitions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Object Type
let thirdObject: object;
thirdObject = {
  color: 'red',
  age: 25
}

// Object Type
let fourthObject: object = {
  color: 'red',
  age: 25
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you take a look at each object definition shown in the snippet above, you will notice that they are defined in a way where we explicitly state the type: &lt;code&gt;object&lt;/code&gt;. While this is okay, it doesn't properly define what type each individual property in the object should hold. So it isn't good practice to use this method as it isn't readable.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;em&gt;Array&lt;/em&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[1, 'second', false]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Arrays in typescript can be &lt;em&gt;strict&lt;/em&gt; or &lt;em&gt;flexible&lt;/em&gt; depending on how we want them.&lt;/p&gt;

&lt;p&gt;Array type can be defined in the following ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Option 1
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let firstArray = []   &amp;lt;--- // any[]

let secondArray = ['one', 'two']   &amp;lt;--- // string[]

let thirdArray = [1, 2]   &amp;lt;--- // number[]

let fourthArray = [true, false]   &amp;lt;--- // boolean[]

let fifthArray = ['one', 1, true]   &amp;lt;--- // (string | number | boolean)[]

let sixthArray: string[]; &amp;lt;--- // A string Array type

sixthArray = ['one', 'two']

let seventhArray: string[] = ['find', 'peace'];

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;firstArray&lt;/code&gt; is a &lt;em&gt;flexible&lt;/em&gt; array as we did not explicitly state what kind of element the array would contain. On the other hand, &lt;code&gt;secondArray&lt;/code&gt;, &lt;code&gt;thirdArray&lt;/code&gt;, and &lt;code&gt;fourthArray&lt;/code&gt; are &lt;em&gt;strict&lt;/em&gt; arrays because the elements in each array are of one single type. &lt;code&gt;fifthArray&lt;/code&gt; contains different element types and so typescript identifies that the array can only contain any of the three types. In this scenario, we can modify our &lt;code&gt;fifthArray&lt;/code&gt; with any element that matches each of the three types: &lt;code&gt;string&lt;/code&gt; or &lt;code&gt;number&lt;/code&gt; or &lt;code&gt;boolean&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Option 2
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let eightArray: Array&amp;lt;number&amp;gt;;

eightArray = [2, 6, 4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The method defined above uses what is called a &lt;code&gt;generic&lt;/code&gt; array type definition. This works the same way &lt;code&gt;let sampleArray: number[];&lt;/code&gt; works. It is also a &lt;em&gt;strict&lt;/em&gt; type array definition. What if we want a strict type where we only want two elements in the array of a defined type. This is where we can use a new type called &lt;code&gt;Tuple&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;em&gt;Tuple&lt;/em&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[1, 'a string']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A tuple is a special type provided by typescript that aims to give us more control over defining and handling elements. Tuple exists in other programming languages like Python but is not available in Javascript.&lt;/p&gt;

&lt;p&gt;A tuple can be defined as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Option 1
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let person: [number, string];

person = [40, 'peterson']    &amp;lt;--- // correct

person = ['peterson', 40]    &amp;lt;--- // ERROR

person = [40, 'peterson', 'james']    // ERROR --&amp;gt; Type '[number, string, string]' is not assignable to type '[number, string]'. Source has 3 element(s) but target allows only 2.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looking at the definition above, we can get an idea of how powerful a &lt;code&gt;Tuple&lt;/code&gt; is. We can define a fixed style of data structure and use it strictly. In the &lt;code&gt;person&lt;/code&gt; tuple, we can only assign elements that match the types defined, and in addition, the elements must be in exactly the same shape as the definition and the same length. Notice that if we try to add three elements to the tuple, we get an error as it's only meant to have two elements.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Option 2
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let identity: [number, string] = [2, 'james'];

identity = [20, 'Eren Yeager']; // Valid
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For option 2, we initialize a value on the spot. A new value can be used to modify the existing tuple, as long as it matches the shape and length.&lt;/p&gt;

&lt;p&gt;Take a look at the code below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let animal = [number, string];

animal = [5, 'goats'];

animal.push('birds');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While a tuple is a fixed-length definition, one caveat is that calling a method like &lt;code&gt;push()&lt;/code&gt; on a tuple would actually work, as typescript does not throw an error when you push. This is something you should keep in mind while using tuples.&lt;br&gt;
The below code is the result of calling &lt;code&gt;push&lt;/code&gt; on the tuple above:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[5, 'goats', 'birds']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Calling a method like &lt;code&gt;push()&lt;/code&gt; on a tuple would actually work, as typescript does not throw an error when you push.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This is something you should keep in mind while using tuples.&lt;/p&gt;
&lt;/blockquote&gt;


&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  &lt;em&gt;Enum&lt;/em&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;enum { VALUE1, VALUE2, ... }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An enum is a user-defined data type. It is made up of named &lt;em&gt;constant&lt;/em&gt; values that are normally referred to as &lt;em&gt;enumerators&lt;/em&gt;, hence the name: &lt;strong&gt;Enum&lt;/strong&gt;. Behind the scenes, the values are held as integers. Enums are natively available in other programming languages, like Java, Python, and others, but are not supported by Javascript natively. If we want to create an enum in javascript, we would define an object of the values with a &lt;code&gt;const&lt;/code&gt; and then call &lt;code&gt;freeze&lt;/code&gt; on the defined object.&lt;/p&gt;

&lt;p&gt;However, in typescript, we have enums out of the box.&lt;/p&gt;

&lt;p&gt;Enums are defined as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;enum Color = {
  RED,
  YELLOW,
  BLUE
}

// To assess it
let paint: Color = Color.RED;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One use case for Enums would be to have pre-defined constant names for role-based authorization. This prevents us from having to type the roles for every single usage. You will mostly find enums written in all caps like &lt;code&gt;BLUE&lt;/code&gt; but this isn't a restriction, as the case it is written in doesn't matter. It can also be written as &lt;code&gt;Blue&lt;/code&gt; and still function the same.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Color.RED&lt;/code&gt; above isn't explicitly assigned a value, so the hidden value defaults to &lt;code&gt;0&lt;/code&gt;, and &lt;code&gt;Color.YELLOW&lt;/code&gt; becomes &lt;code&gt;1&lt;/code&gt;, and so on. You catch the drift 😁.&lt;/p&gt;

&lt;p&gt;You can explicitly define the value, just like we did below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;enum Color = {
  RED = 100,
  YELLOW,
  BLUE
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, &lt;code&gt;Color.RED&lt;/code&gt; takes on the value of &lt;code&gt;100&lt;/code&gt;, and since the values of &lt;code&gt;YELLOW&lt;/code&gt; and &lt;code&gt;BLUE&lt;/code&gt; aren't explicitly defined, they would become &lt;code&gt;101&lt;/code&gt; and &lt;code&gt;102&lt;/code&gt; respectively.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You can assign whatever value you want to an Enum. The values aren't restricted to just numbers, they could also be strings.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;:
&lt;/h2&gt;

&lt;p&gt;Typescript, which was first introduced in 2012 by Microsoft has become an incredible tool for Javascript developers, permitting type checking during development to prevent unforeseen errors. As more developers and organizations continue to adopt it and in addition to it being open-source, its popularity is bound to keep increasing. Whether you choose to use it in your project or not is entirely up to you.&lt;/p&gt;

&lt;p&gt;If you however choose to adopt it, you would find a wealth of information on the &lt;a href="https://www.typescriptlang.org/docs/"&gt;Official Typescript Documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If there are other typescript topics you'd like me to write on, you can drop a comment below 👇&lt;/p&gt;

&lt;p&gt;Till next time! Ciao! 👋&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>typescript</category>
      <category>code</category>
    </item>
  </channel>
</rss>
