<?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>Forem: Guilherme Niches</title>
    <description>The latest articles on Forem by Guilherme Niches (@gniches).</description>
    <link>https://forem.com/gniches</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%2F968216%2F1b8b3305-0e52-4760-86e4-00ef4ebcbfc0.jpeg</url>
      <title>Forem: Guilherme Niches</title>
      <link>https://forem.com/gniches</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/gniches"/>
    <language>en</language>
    <item>
      <title>Clone your Github repositories using shell script</title>
      <dc:creator>Guilherme Niches</dc:creator>
      <pubDate>Mon, 08 May 2023 22:11:00 +0000</pubDate>
      <link>https://forem.com/gniches/clone-yours-github-repositories-using-shell-script-3o33</link>
      <guid>https://forem.com/gniches/clone-yours-github-repositories-using-shell-script-3o33</guid>
      <description>&lt;p&gt;It's a common scenario we lost our local projects because of problems in our machine. Our Laptop/Desktop can break down and we lost a lot of data. It's a painful situation, recovery not only all data, photos, software's, etc. But, recovery all our repositories too. &lt;/p&gt;

&lt;p&gt;To our happiness, all repositories are storage/save in cloud through GitHub if we use Github as hosting service.&lt;/p&gt;

&lt;p&gt;However, if we work simultaneously on various projects could be a little boring clone all repositories one-by-one.&lt;/p&gt;

&lt;p&gt;Automate tasks is a great way to make a better use of our time. And shell script is a good resource to achieve this type of automation.&lt;/p&gt;

&lt;p&gt;In this article we will create a shell script that automatically clone all repositories of your GitHub account. &lt;/p&gt;




&lt;h3&gt;
  
  
  Shell Script
&lt;/h3&gt;

&lt;p&gt;Shell script is a script language used in command line environment. It allow us automate terminal commands in Unix based systems like Linux and MacOS.&lt;/p&gt;

&lt;h3&gt;
  
  
  Let's start
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;This article assumes that you have a basic knowledge about terminal commands, so is not the focus here explain what instructions like touch, mkdir, etc do.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First of all, we need to create a new script file.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 clone-github-repositories.sh

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

&lt;/div&gt;

&lt;p&gt;Note that the ".sh" is the extension of shell script files, without it the interpreter cannot understand that this file is a shell script.&lt;/p&gt;

&lt;p&gt;After, we need open this file in a text editor of your preference (vi, nano, etc).&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 clone-github-repositories.sh

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

&lt;/div&gt;

&lt;p&gt;In the editor we need write in the first line something very important to our script: indicate the interpreter of the script. In my case, I will use bash, but, exists others like sh, ksh, etc.&lt;/p&gt;

&lt;p&gt;So, the first line of our script will be the following:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 #!/bin/bash 

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

&lt;/div&gt;

&lt;p&gt;We will create a directory where we'll clone all our repositories. I will call this directory "projects", but you can name it like you want.&lt;/p&gt;

&lt;p&gt;So the next line of our script will be the this command:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 mkdir projects 

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

&lt;/div&gt;

&lt;p&gt;Like I said, we will clone our repositories inside projects directory, so we need enter to the projects directory, the following line of script will be:&lt;/p&gt;

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

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

&lt;/div&gt;

&lt;p&gt;Okay, so until now we have some simple instructions in our script, we need have access to all repositories from our GitHub account, we could go to our GitHub profile and copy the URL repositories one-by-one, but,  this is not an agile way, right? Here, enter in scene the GitHub REST API.&lt;/p&gt;

&lt;p&gt;The GitHub REST API allow us make integrations, automation and much more.&lt;/p&gt;

&lt;p&gt;Our next line in script will be a request to GitHub API using curl:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 -s https://api.github.com/users/{your-github-username}/repos 

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

&lt;/div&gt;

&lt;p&gt;Replace {your-github-username} by your GitHub username.&lt;/p&gt;

&lt;p&gt;If you run this command solo in terminal the return will be a JSON content with a array of objects containing all properties of your repositories. Below an example of the JSON that return from the above command.&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%2Ftnwpzmvb7pdfujb3wfit.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%2Ftnwpzmvb7pdfujb3wfit.png" alt="Github API JSON return"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Between the many properties we have two especially important: ssh_url and clone_url.&lt;/p&gt;

&lt;p&gt;This properties contains the URL's that we can use to clone the repositories. If you use ssh feature to clone your projects, you can use the "ssh_url", else, the "clone_url" will work the same way. In my case, I use SSH to clone my repositories.&lt;/p&gt;

&lt;p&gt;Now, we have to work with some resources to manipulate content of files, because we don't need use all information that return from GitHub API.&lt;/p&gt;

&lt;p&gt;Here, "grep" is our best friend. grep will search inside a file by a string/pattern that we inform.&lt;/p&gt;

&lt;p&gt;We will refactor our script line to the following:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 curl -s https://api.github.com/users/{your-github-username}/repos | grep ssh_url 

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

&lt;/div&gt;

&lt;p&gt;Remember that if you don't use SSH you must change the "ssh_url" by "clone_url".&lt;/p&gt;

&lt;p&gt;The output of this instruction will be something like this (if you run this directly in terminal):&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%2Fvyf46fh957j1sjjmfgun.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%2Fvyf46fh957j1sjjmfgun.png" alt="grep return"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To clone a project we need only the URL, so we need filter this grep return.&lt;/p&gt;

&lt;p&gt;We will use the command "cut" with a delimiter to remove the unnecessary sections from the lines.&lt;/p&gt;

&lt;p&gt;Refactoring our instruction, now the command will be this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 curl -s https://api.github.com/users/{your-github-username}/repos | grep ssh_url | cut -d'"' -f 4 

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

&lt;/div&gt;

&lt;p&gt;-d is a flag for field delimiter and we use the double quotes (") as delimiter.&lt;/p&gt;

&lt;p&gt;-f is a flag for fields that select only the field (or range of fields) informed. And 4 is the specified index of field line that we want extract.&lt;/p&gt;

&lt;p&gt;If you run this instruction in terminal the output is 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%2Ffhm4gnf95x03mugtkv66.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%2Ffhm4gnf95x03mugtkv66.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, we have the exact URL to clone our repositories.&lt;/p&gt;

&lt;p&gt;So, we will refactor the line to save the output list in a variable.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 repos=$(curl -s https://api.github.com/users/{your-github-username}/repos | grep ssh_url | cut -d'"' -f 4) 

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

&lt;/div&gt;

&lt;p&gt;repos is the name of variable and the "$" symbol indicate to the shell that this is a variable, we have a list of URL's in this variable.&lt;/p&gt;

&lt;p&gt;We will need apply some programming logic to do the task, iterable the repos list and for each repository of the list we will clone the repository.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 for repo in $repos
do
  git clone $repo
done 

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

&lt;/div&gt;

&lt;p&gt;The full script is below:&lt;/p&gt;

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

#!/bin/bash

mkdir projects
cd projects

repos=$(curl -s https://api.github.com/users/gniches/repos | grep ssh_url | cut -d'"' -f 4)

for repo in $repos 
do
    git clone $repo
done



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

&lt;/div&gt;

&lt;p&gt;It's done. Now we need run the script to test if our script is working as expected.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 bash clone-github-repositories.sh 

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

&lt;/div&gt;

&lt;p&gt;If everything is ok, now you have a directory called projects with all your repositories inside.&lt;/p&gt;




&lt;h1&gt;
  
  
  Concluding
&lt;/h1&gt;

&lt;p&gt;In this article we learned how to create a shell script to clone all our repositories from GitHub account using GitHub API.&lt;/p&gt;

&lt;p&gt;Shell script is particularly useful to automate process and tasks, like monitoring servers, create periodic backups of databases, send e-mails and much more.   &lt;/p&gt;

&lt;p&gt;Reviews, suggestions and/or contributions to this article are encouraged and welcome.&lt;/p&gt;




&lt;h3&gt;
  
  
  References
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://docs.github.com/pt/rest?apiVersion=2022-11-28" rel="noopener noreferrer"&gt;GitHub REST API Documentation&lt;/a&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>automation</category>
      <category>github</category>
      <category>productivity</category>
    </item>
    <item>
      <title>How to manage multiple GitHub accounts on one computer with SSH keys</title>
      <dc:creator>Guilherme Niches</dc:creator>
      <pubDate>Fri, 10 Mar 2023 16:31:32 +0000</pubDate>
      <link>https://forem.com/gniches/how-to-manage-multiple-github-accounts-on-one-computer-with-ssh-keys-57j5</link>
      <guid>https://forem.com/gniches/how-to-manage-multiple-github-accounts-on-one-computer-with-ssh-keys-57j5</guid>
      <description>&lt;p&gt;In our journey through the world of programming it is very common to have to create more than one account on Github. &lt;/p&gt;

&lt;p&gt;Either to have a personal account, which we use for our own projects, and a professional account linked to the company where we work. Either to have separate personal accounts for different purposes.&lt;/p&gt;

&lt;p&gt;The question is, how can we use two or more different Github accounts on a single computer?&lt;/p&gt;

&lt;p&gt;One of the possible answers is: using &lt;strong&gt;SSH keys&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  SSH Keys
&lt;/h2&gt;




&lt;h3&gt;
  
  
  What is it?
&lt;/h3&gt;

&lt;p&gt;SSH (Secure Shell) or Key Pair is a tool of remote access with focus on security, it's the means of identification between your computer and an SSH server using public encryption key authentication method.&lt;/p&gt;

&lt;p&gt;One of it's advantages is that it does not transmit information such as login and password, so if your request is intercepted by malicious agents, they will not have access to your credentials.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: &lt;em&gt;It's not the purpose of this article to explain in depth how the SSH keys works and it's types of encryption. However, at the end of the article I will put some links that help us to understand the concept in more depth.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note 2&lt;/strong&gt;: &lt;em&gt;This article will focus on Linux/Mac commands, if you use Windows you can install Git Bash to emulate a Unix Shell. Git Bash is a build-in package of &lt;a href="https://git-scm.com/download/win" rel="noopener noreferrer"&gt;Git for Windows&lt;/a&gt;. Or, in some modern Windows environments is possible to use &lt;a href="https://learn.microsoft.com/pt-br/windows/wsl/install" rel="noopener noreferrer"&gt;Windows Subsystem for Linux (WSL)&lt;/a&gt; to reproduce a Linux system inside Windows. After installing Git Bash or WSL you can follow the below steps to reach the same result that a Linux/Mac user.&lt;/em&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Hands On
&lt;/h3&gt;

&lt;p&gt;Okay, so now we know a little more about SSH keys and why use it let's start to configure our SSH to management two or more Github account.&lt;/p&gt;

&lt;p&gt;We will reproduce the most common scenario: When you have a personal GitHub account for your personal projects and a company/work account.&lt;/p&gt;

&lt;p&gt;I will suppose that you have the basic knowledge of terminal commands such as navigation between directories, create and edit files, etc.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ssh directory&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The ssh directory is usually located in the main directory of &lt;br&gt;
user: /home/yourUser&lt;/p&gt;

&lt;p&gt;Is a occult directory, so, if you want list the directory, run the ls command with the -a flag to list all files and directories including the occult ones.&lt;/p&gt;

&lt;p&gt;Open your terminal and type the command below to access ssh directory.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Create SSH Keys &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, we will create our first SSH key for our personal account. In terminal run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ssh-keygen -t rsa -C "your-personal@email" -f "github-personal"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace "your-personal@email" by your personal e-mail account. You can replace the "github-personal" too, but the name you put here will be used later, so take this in mind in the next steps.&lt;/p&gt;

&lt;p&gt;Repeat the same command before, changing the "your-personal@email" to your work e-mail account, to create a new SSH key to your professional account.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ssh-keygen -t rsa -C "your-work@email" -f "github-work"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Add the SSH Keys to your SSH-agent&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After create our two SSH keys we will add this keys to the SSH-agent. Type in terminal the following commands to add our personal and work accounts.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ssh-add ~/.ssh/github-personal
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;








&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ssh-add ~/.ssh/github-work
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As said before, the above commands will be slightly differents if you set another value to the "github-personal" or "github-work" in the creation command.&lt;/p&gt;

&lt;p&gt;To test if the SSH keys were correctly added to the SSH agent, run the below command to list your active keys by fingerprint:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ssh-add -l 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output should be similar to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;3072 SHA256:a lot of random characters your-personal@email (RSA)

3072 SHA256:a lot of random characters your-work@email (RSA)  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Add to Github account&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here we have two options.&lt;/p&gt;

&lt;p&gt;1) We can set our two SSH keys to our personal account, in this way, you can access your personal account by the two SSH keys (personal and work keys). The same is valid if you set the two keys to the work account.&lt;/p&gt;

&lt;p&gt;2) Or, we can set just the personal key to personal account and the work key to the work account, in this way, your personal key will not have access to the your work account and vice versa.&lt;/p&gt;

&lt;p&gt;I personally, prefer to keep the things separated. So I will follow the second option.&lt;/p&gt;

&lt;p&gt;The next step will be copy your public key and link to your Github account. &lt;em&gt;In Mac&lt;/em&gt;, type the below command to copy the public ssh key.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pbcopy &amp;lt; ~/.ssh/github-work.pub
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;








&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pbcopy &amp;lt; ~/.ssh/github-personal.pub
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;In Linux&lt;/em&gt;, you need use some tool like &lt;strong&gt;xclip&lt;/strong&gt; or just open the created files with any text editor or &lt;em&gt;cat&lt;/em&gt; command and copy all the content of files.&lt;/p&gt;

&lt;p&gt;The content of this files should be similar to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ssh-rsa [...long list of random characters]= your-work@email
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, you can go to Github webpage, signup in your account and access settings in profile menu.&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%2Ffz7fgm5eox1ry3xs2aj8.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%2Ffz7fgm5eox1ry3xs2aj8.png" alt="GitHub Profile Settings"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In settings page, go to &lt;em&gt;SSH and GPG Keys&lt;/em&gt; in navigation menu.&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%2Ffblctswgawz5m922waa7.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%2Ffblctswgawz5m922waa7.png" alt="SSH and GPG keys"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, will be listed all SSH keys that you already have associated to your account.&lt;/p&gt;

&lt;p&gt;Click in the &lt;em&gt;"New SSH key"&lt;/em&gt; green button and you will be redirect to the page 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%2Fozp21fujgk0zvhelq1ly.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%2Fozp21fujgk0zvhelq1ly.png" alt="SSH keys / Add new"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this page we will paste in the Key field the SSH public key that we copy in the last step. Set a Title to this key and click in the green button &lt;em&gt;"Add SSH key"&lt;/em&gt;. Back in the previous page, the new created SSH key should be listed.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create Config file&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In terminal, create a new file called config.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;touch ~/.ssh/config
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;In Mac&lt;/em&gt;, type this command to open the config file created:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;open -e ~/.ssh/config
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;In Linux&lt;/em&gt;, you can use vim, nano or other text editor that you like.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nano config
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside config file paste the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Personal GitHub account
Host github.com-personal
 HostName github.com
 User git
 IdentityFile ~/.ssh/github-personal

# Work GitHub account
Host github.com-work
 HostName github.com
 User git
 IdentityFile ~/.ssh/github-work

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

&lt;/div&gt;



&lt;p&gt;Save the file and exit.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Make default account global&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Set one of the accounts as default. In terminal, type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git config --global user.name "personal"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git config --global user.email "your-personal@email"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Clone repository with the secondary account&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I will clone a repository from my professional account.&lt;/p&gt;

&lt;p&gt;Go to your Github profile, open a repository, click &lt;em&gt;"Code"&lt;/em&gt; button, select SSH tab and copy the SSH link. (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%2Fstp42h5avkbfu3tuh685.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%2Fstp42h5avkbfu3tuh685.png" alt="Clone by SSH"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here we got a point of attention: the default SSH link provided by Github has this format:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git@github.com:repo-owner-user-name/the-repo-name.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What we will do is a little change in this format adding the Host as we defined in the config file of our SSH, the SSH link will be similar to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git@github.com-work:repo-owner-user-name/the-repo-name.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, to clone the repository the complete command will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git clone git@github.com-work:repo-owner-user-name/the-repo-name.git my-repo
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;"my-repo" is just the name of the directory where the repository will be clone inside, you can put the name you want, or putting nothing to clone the repository inside the directory where you are.&lt;/p&gt;

&lt;p&gt;Go to my-repo directory, type:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;If you run: &lt;em&gt;git config user.name&lt;/em&gt; in terminal, you will receive as answer the default global value configured, in my case, was the personal. The same is valid to &lt;em&gt;git config user.email&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;So, for this repository that was cloned from my work account I want to use my professional credentials. To do this, just type in terminal, inside the directory of the repository:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git config user.name "work"
git config user.email "your-work@email"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This commands will be set your professional credentials to this specific repository from work and you will be able to perform git actions to this repository using your work account.&lt;/p&gt;

&lt;p&gt;If you set your global credentials to the personal account and clone a personal repository, you don't need specify the user.name and user.email to this repository, because the default credential is your personal account and the git actions that you perform in this repository will be associated to your personal account. The same is valid if you set your work account as global and clone a work repository. &lt;/p&gt;




&lt;h2&gt;
  
  
  Concluding
&lt;/h2&gt;

&lt;p&gt;We have seen how to create, configure and manage multiple Github accounts on one machine using SSH keys, this is very useful when we need different accounts for distinct purposes.&lt;/p&gt;

&lt;p&gt;Reviews, suggestions and/or contributions to this article are encouraged and welcome.&lt;/p&gt;




&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.ssh.com/academy/ssh" rel="noopener noreferrer"&gt;What is SSH?&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://rcic.uci.edu/hpc3/ssh-guide.html#_ssh_keys_concepts" rel="noopener noreferrer"&gt;SSH Keys Concepts&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.atlassian.com/git/tutorials/git-ssh" rel="noopener noreferrer"&gt;Git SSH Keys&lt;/a&gt;&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>beginners</category>
      <category>github</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Start and Setup a NodeJs Typescript Project</title>
      <dc:creator>Guilherme Niches</dc:creator>
      <pubDate>Mon, 06 Feb 2023 12:18:49 +0000</pubDate>
      <link>https://forem.com/gniches/start-and-setup-a-nodejs-typescript-project-27nl</link>
      <guid>https://forem.com/gniches/start-and-setup-a-nodejs-typescript-project-27nl</guid>
      <description>&lt;p&gt;Start a new project usually involves a serie of commands and not always we remember all this commands and the main dependencies that we normally use.&lt;/p&gt;

&lt;p&gt;This article show us one of many ways to start and setup a new NodeJs Typescript project.&lt;/p&gt;

&lt;p&gt;Before this, some fast definitions of the tools we will use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;NodeJs&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;"NodeJs is an open-source, cross-platform JavaScript runtime environment."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In other words, NodeJs allow us use Javascript in the server-side, without the need of a web browser.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Typescript&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Typescript is a superset of JavaScript, this basically means that the Typescript adds static typing on top of JavaScript.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note: This article assumes that you already have NodeJS and some package manager installed on your machine. If you doesn't, check this links first:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.npmjs.com/downloading-and-installing-node-js-and-npm"&gt;NodeJs/NPM&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://yarnpkg.com/getting-started/install"&gt;Yarn&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Commands to start a NodeJS TS Project
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;TS/Node dependencies
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add typescript ts-node ts-node-dev @types/node 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;OR&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i typescript ts-node ts-node-dev @types/node
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  tsconfig.json
&lt;/h2&gt;

&lt;p&gt;Create &lt;strong&gt;tsconfig.json&lt;/strong&gt; (Visit &lt;a href="https://aka.ms/tsconfig.json"&gt;https://aka.ms/tsconfig.json&lt;/a&gt; to read more about this file) file in the root directory of your project with the following structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "compilerOptions": {
    /*Basic Options*/
    "target": "es2018",   /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', 'ES2021', or 'ESNEXT'. */
    "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */   
    "allowJs": true,      /* Allow javascript files to be compiled. */    
    "outDir": "./dist",   /* Redirect output structure to the directory. */
    "rootDir": "./src",   /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */  

    /* Strict Type-Checking Options */
    "strict": true,       /* Enable all strict type-checking options. */    
    "esModuleInterop": true,   /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */    

    /* Advanced Options */
    "skipLibCheck": true,  /* Skip type checking of declaration files. */
    "forceConsistentCasingInFileNames": true   /* Disallow inconsistently-cased references to the same file. */
  }
}

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

&lt;/div&gt;



&lt;p&gt;According to this configuration you should write your code in &lt;strong&gt;src/&lt;/strong&gt; folder and should have an entry point file called &lt;strong&gt;server.ts&lt;/strong&gt; inside.&lt;/p&gt;

&lt;p&gt;Our project structure should be similar to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
├── src
     ├── server.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside the file &lt;strong&gt;server.ts&lt;/strong&gt; we could put a simple log just to got a return when we run the server.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log("Server is Up");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  package.json
&lt;/h2&gt;

&lt;p&gt;Add &lt;strong&gt;dev&lt;/strong&gt;, &lt;strong&gt;dev:debug&lt;/strong&gt; and &lt;strong&gt;build&lt;/strong&gt; scripts to your &lt;strong&gt;package.json&lt;/strong&gt; file, &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Dev&lt;/strong&gt;: Uses &lt;strong&gt;ts-node-dev&lt;/strong&gt; to start a dev server that automatically transpiles your &lt;strong&gt;.ts&lt;/strong&gt; files to &lt;strong&gt;.js&lt;/strong&gt; whenever you edit something. (More info at &lt;a href="https://www.npmjs.com/package/ts-node-dev"&gt;https://www.npmjs.com/package/ts-node-dev&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Build&lt;/strong&gt;: Uses &lt;strong&gt;tsc&lt;/strong&gt; to transpile your whole application to &lt;strong&gt;.js&lt;/strong&gt;, is used when you want to deploy to production. (More info at &lt;a href="https://www.typescriptlang.org/docs/handbook/compiler-options.html"&gt;https://www.typescriptlang.org/docs/handbook/compiler-options.html&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Dev:debug&lt;/strong&gt;: Uses &lt;strong&gt;ts-node-dev --inspect&lt;/strong&gt; to start a debug function at port defined after '=' in inspect flag.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"dev"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"ts-node-dev --respawn src/server.ts"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"dev:debug"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"ts-node-dev --inspect=0.0.0.0:9229 src/server.ts"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"build"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"tsc"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"start"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"node dist/server.js"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Run
&lt;/h2&gt;



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

&lt;/div&gt;



&lt;p&gt;OR&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Dev:debug
&lt;/h2&gt;



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

&lt;/div&gt;



&lt;p&gt;OR&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn dev:debug
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Build
&lt;/h2&gt;



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

&lt;/div&gt;



&lt;p&gt;OR&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Optional
&lt;/h2&gt;

&lt;p&gt;These steps above are enough to start and run a NodeJS Typescript project, it's a 'clean' install. But, normally, we want start a project with some dev dependencies.&lt;/p&gt;

&lt;p&gt;The following steps are optional and will install some dependencies commonly used with NodeJS.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Express =&amp;gt; NodeJs framework to handling with routes (&lt;a href="http://expressjs.com/"&gt;http://expressjs.com/&lt;/a&gt;)
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i express --save
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;OR&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add express -D
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Sequelize or TypeORM =&amp;gt; ORMs (&lt;a href="https://sequelize.org/"&gt;https://sequelize.org/&lt;/a&gt;) || (&lt;a href="https://typeorm.io/#/"&gt;https://typeorm.io/#/&lt;/a&gt;)
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i sequelize --save
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;OR&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i typeorm --save
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;OR&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add sequelize -D
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;OR&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add typeorm -D
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Some other dependencies often used with NodeJS/Typescript project:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Eslint =&amp;gt; Identify code patterns in disagreement with pre-established rules (&lt;a href="https://eslint.org/"&gt;https://eslint.org/&lt;/a&gt;) &lt;/li&gt;
&lt;li&gt;PostgreSql or MongoDB =&amp;gt; Relational Database and NoSql Database (MongoDb)&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Extra
&lt;/h2&gt;

&lt;h3&gt;
  
  
  NPM Package
&lt;/h3&gt;

&lt;p&gt;If you are writing a npm package specify &lt;strong&gt;main&lt;/strong&gt; and &lt;strong&gt;types&lt;/strong&gt; path and add &lt;strong&gt;--declaration&lt;/strong&gt; to your build script in order to generate types suggestions to who is using your package.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;package.json&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"main"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"dist/index.js"&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nl"&gt;"types"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"dist/index.d.ts"&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"build"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"tsc --declaration"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Concluding
&lt;/h2&gt;

&lt;p&gt;This is one of many ways to start and setup a new NodeJs Typescript project, you can do the same in different ways even faster. But, I like to do in this way because is more "step-by-step" form. &lt;/p&gt;

&lt;p&gt;Reviews, suggestions and/or contributions to this article are encouraged and welcome.&lt;/p&gt;




&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://nodejs.org/en/"&gt;NodeJS&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.typescriptlang.org/"&gt;Typescript&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>typescript</category>
      <category>node</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Utility Types, in Typescript.</title>
      <dc:creator>Guilherme Niches</dc:creator>
      <pubDate>Tue, 20 Dec 2022 00:17:32 +0000</pubDate>
      <link>https://forem.com/gniches/utility-types-in-typescript-nal</link>
      <guid>https://forem.com/gniches/utility-types-in-typescript-nal</guid>
      <description>&lt;p&gt;In our day routine as software developer we usually needs to manipulate data and types for multiple purposes: to update a entry in database, to mapping a DTO (Data Transfer Object), to structure our return to the client and much more.&lt;/p&gt;

&lt;p&gt;In Typescript, we have some resources that help us to handling with this &lt;em&gt;Object Type Transformations&lt;/em&gt; called &lt;strong&gt;Utility Types&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Okay, but what's &lt;em&gt;Object Type Transformations&lt;/em&gt;?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Object Type Transformations refers to generating a modified type from an existing type or interface that represents an object." (Source: &lt;a href="https://refine.dev/blog/typescript-pick-utility-type/#introduction" rel="noopener noreferrer"&gt;Refine.dev&lt;/a&gt;)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Cool, now we know about this Typescript resources and about Object Type Transformations. So, let's see some examples?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note: Some utility Types described here are available only from Typescript 3.5 or upper.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Utility Types of Typescript
&lt;/h2&gt;

&lt;p&gt;Typescript has a great diversity of types that can help with some common type manipulation, in this article we will see some of them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Partial
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Changes all the properties in an object to be optional.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Syntax definition:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Partial&amp;lt;T&amp;gt; = {[A in keyof T]?: T[A]};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Right, but what does the above example mean?&lt;/p&gt;

&lt;p&gt;We know that &lt;strong&gt;T&lt;/strong&gt; is the type passed to Partial, in the below example is &lt;strong&gt;IUser&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface IUser {
  uid: string;
  name: string;
  age: number;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, &lt;strong&gt;keyof T&lt;/strong&gt; in our example will be &lt;strong&gt;keyof IUser&lt;/strong&gt;, in other words, &lt;strong&gt;keyof IUser&lt;/strong&gt; it's equivalent of the keys of our object (in our example: "uid" | "name" | "age").&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[A in "uid" | "name" | "age"]?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hence, &lt;strong&gt;A&lt;/strong&gt; must be any of the property keys of the IUser type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;['uid']? = IUser['uid']
['name']? = IUser['name']
['age']? = IUser['age']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All keys with the question mark (?) are to show that the property key is optional. Then, it's like the Partial generate other IUser interface (called PartialIUser in our example below) with all optional keys.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface PartialIUser {
  uid?: string;
  name?: string;
  age?: number;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Usecase Example
&lt;/h2&gt;

&lt;p&gt;In the below example, the function &lt;strong&gt;updateUser&lt;/strong&gt; would accept any user fields, ie, the User could be partially updated. And we can now use this function to update each field individually without needing the other ones to be set.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fm3ng7t2cyd7e80bkt3s1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fm3ng7t2cyd7e80bkt3s1.png" alt="Image description" width="800" height="473"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Omit
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Removes keys from an object type.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Syntax definition:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Omit&amp;lt;T, K extends keyof any&amp;gt; = Pick&amp;lt;T, Exclude&amp;lt;keyof T, K&amp;gt;&amp;gt;;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Maybe you noted, the Omit type is based on using other two utility types: &lt;strong&gt;Pick&lt;/strong&gt; and Exclude (we are talk about &lt;strong&gt;Pick&lt;/strong&gt; later).&lt;/p&gt;

&lt;p&gt;Omit definition is equal to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; Pick&amp;lt;T, Exclude&amp;lt;keyof T, K&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using our IUser interface from previous example, again, &lt;strong&gt;T&lt;/strong&gt; is our IUser and &lt;strong&gt;K&lt;/strong&gt; is a string literal of keys that are removed from &lt;strong&gt;T&lt;/strong&gt;, ie, our IUser.&lt;/p&gt;

&lt;p&gt;Let's see a example.&lt;/p&gt;




&lt;h2&gt;
  
  
  Usecase Example
&lt;/h2&gt;

&lt;p&gt;In the following example, we use Omit to remove the key "age" from our IUser interface and set the new type &lt;strong&gt;IUserWithoutAge&lt;/strong&gt; to a new variable. In this variable, the property "age" is not available and can't be used.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fwb34fy0c96mrrivng5wg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fwb34fy0c96mrrivng5wg.png" alt="Image description" width="800" height="782"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note that it's possible omit multiple keys from an object, passing a union of string literals in &lt;strong&gt;K&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type IUserWithoutAgeAndName = Omit&amp;lt;IUser, "age" | "name"&amp;gt;;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Pick
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;"Keeps" only the specified keys. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Syntax definition:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Pick&amp;lt;T, K extends keyof T&amp;gt; = {
    [P in K]: T[P];
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pick will generate a new object. So, it's not possible to have a primitive type (string or number, for example) as the new type.&lt;/p&gt;

&lt;p&gt;Breaking down the syntax definition and using our IUser interface as example we have the following: &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;T[P]&lt;/strong&gt; is a way to access the type of a property. So, if we want access the "age" property from our IUser interface we can do like this: &lt;strong&gt;IUser['age']&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Again, &lt;strong&gt;keyof T&lt;/strong&gt; are the keys of our type, in this example IUser interface and the keys "uid", "name" and "age".&lt;/p&gt;

&lt;p&gt;And we have &lt;strong&gt;[P in K]&lt;/strong&gt; which means that property P needs be one of the keys extracted from K. Then, P could be either "uid", "name" or "age".&lt;/p&gt;




&lt;h2&gt;
  
  
  Usecase Example
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fn18j63wtydpam2a9v8qb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fn18j63wtydpam2a9v8qb.png" alt="Image description" width="800" height="738"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the example above, we picked the key "age", so, the variable that has the type IUserWithOnlyAge has only the key "age" to be setted, the others properties "uid" and "name" hasn't available.&lt;/p&gt;




&lt;h2&gt;
  
  
  Readonly
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Turn all properties readonly.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Syntax definition:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Readonly&amp;lt;T&amp;gt; = {
    readonly [P in keyof T]: T[P];
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Understanding the syntax definition:&lt;/p&gt;

&lt;p&gt;Like the previous examples, &lt;strong&gt;T[P]&lt;/strong&gt; is a way to access the type of a property and &lt;strong&gt;keyof T&lt;/strong&gt; are the keys of our type.&lt;br&gt;
The &lt;strong&gt;readonly&lt;/strong&gt; prefix is a keyword of typescript that makes a property as read-only in the class, type or interface.&lt;/p&gt;

&lt;p&gt;So, basically, for each property of our type we are turning this property readonly. &lt;/p&gt;




&lt;h2&gt;
  
  
  Usecase Example
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Ftrkebe2q7edlmdljy6ms.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Ftrkebe2q7edlmdljy6ms.png" alt="Image description" width="800" height="966"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above example show us that try reassign the variable &lt;strong&gt;newUser&lt;/strong&gt; with has &lt;strong&gt;IUserReadonly&lt;/strong&gt; type will cause a compilation error.&lt;/p&gt;




&lt;h2&gt;
  
  
  Concluding
&lt;/h2&gt;

&lt;p&gt;In this article we just scratch the surface of utility types of Typescript. Has a lot more utility types that can be very useful for different usecases and you can discovery it in Typescript documentation. &lt;/p&gt;

&lt;p&gt;Utility types help us to handling with transform and manipulate data and can improve our code turning it more clean, should be used wisely.&lt;/p&gt;

&lt;p&gt;Let your comment if you already used utility types.&lt;/p&gt;

&lt;p&gt;Reviews, suggestions and/or contributions to this article are encouraged and welcome. &lt;/p&gt;




&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.typescriptlang.org/docs/handbook/utility-types.html" rel="noopener noreferrer"&gt;Typescript Documentation&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://blog.devgenius.io/types-transformations-with-typescript-167d807adf25" rel="noopener noreferrer"&gt;Types Transformations With Typescript&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://refine.dev/blog/typescript-pick-utility-type/#introduction" rel="noopener noreferrer"&gt;A Detailed Guide on Typescript Guide Pick Type&lt;/a&gt;&lt;/p&gt;

</description>
      <category>emptystring</category>
    </item>
    <item>
      <title>Function with dynamically typed parameters, in Typescript.</title>
      <dc:creator>Guilherme Niches</dc:creator>
      <pubDate>Tue, 08 Nov 2022 14:52:00 +0000</pubDate>
      <link>https://forem.com/gniches/function-with-dynamically-typed-parameters-in-typescript-2n6l</link>
      <guid>https://forem.com/gniches/function-with-dynamically-typed-parameters-in-typescript-2n6l</guid>
      <description>&lt;h3&gt;
  
  
  In Typescript, how to create a function that returns the type that itself receives?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;If we don't explicitly set the parameter the function returns an 'any' type.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If we set it, the function returns the fixed type in the parameter.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, how to handle it? Let's see.&lt;/p&gt;




&lt;p&gt;In the below example, we declare a function that receives a parameter and returns the parameter itself. This parameter was not explicitly set, so when we call the function, the returned value has an 'any' type.&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%2Fy2pkjsk2k6ju9prilnga.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%2Fy2pkjsk2k6ju9prilnga.png" alt="example.ts"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But, wait... We passed a number as a parameter to our function and this function just returns this parameter, so, the value should be a number, right? Well, unfortunately, the Typescript "doesn't understand" it in this way.&lt;/p&gt;

&lt;p&gt;To handle with this, we can use &lt;strong&gt;Generics&lt;/strong&gt; of Typescript.&lt;/p&gt;




&lt;h3&gt;
  
  
  Generics in Typescript
&lt;/h3&gt;

&lt;p&gt;Okay, so we will use Generics, but what is Generics?&lt;/p&gt;

&lt;p&gt;The Typescript documentation explains Generics as: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"(...) being able to create a component that can work on multiple types rather than a single one."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Refactoring the code, our function now received the &lt;em&gt;type variable&lt;/em&gt; &lt;strong&gt;Type&lt;/strong&gt;, a kind of variable that works on types rather than values. This &lt;strong&gt;Type&lt;/strong&gt; allow us to capture the type that the user inform. And in this example, we declare this &lt;strong&gt;Type&lt;/strong&gt; as the type of the return of the function.&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%2F1b9iwifnqz9autmvyu7b.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%2F1b9iwifnqz9autmvyu7b.png" alt="example.ts"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, if we pass a number to our function it will return a number, if we pass a string it will return a string, if we pass an object with some attributes (like as example above), it will return the object with the typed values. The same goes for array and other types.&lt;/p&gt;




&lt;h3&gt;
  
  
  Concluding
&lt;/h3&gt;

&lt;p&gt;In this way, our function will apply types regardless of what we pass as a parameter. This allows us to create functions that can be used in different contexts, turning the function more reusable.&lt;/p&gt;

&lt;p&gt;It is worth mentioning that, despite this feature of Typescript, it is always interesting to try to be declarative in your typing. So, this is a feature to use with prudence.&lt;/p&gt;




&lt;h4&gt;
  
  
  References
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.typescriptlang.org/docs/handbook/2/generics.html" rel="noopener noreferrer"&gt;Typescript Documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://betterprogramming.pub/typescript-generics-90be93d8c292" rel="noopener noreferrer"&gt;Understanding Typescript Generics - Eng&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>typescript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
