<?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: Yuriy Markov</title>
    <description>The latest articles on Forem by Yuriy Markov (@peacefullatom).</description>
    <link>https://forem.com/peacefullatom</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%2F270314%2F2658d38c-81b0-4459-84eb-ea18d9b78afb.jpeg</url>
      <title>Forem: Yuriy Markov</title>
      <link>https://forem.com/peacefullatom</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/peacefullatom"/>
    <language>en</language>
    <item>
      <title>Inferring types with TypeScript</title>
      <dc:creator>Yuriy Markov</dc:creator>
      <pubDate>Tue, 04 Feb 2020 09:55:51 +0000</pubDate>
      <link>https://forem.com/peacefullatom/inferring-types-with-typescript-456j</link>
      <guid>https://forem.com/peacefullatom/inferring-types-with-typescript-456j</guid>
      <description>&lt;p&gt;TypeScript provides advanced tools for work with types. In this post, I will show you how you can infer the data provided in the base class.&lt;/p&gt;

&lt;p&gt;The setup is simple: we will create a base component, a function that will create objects of a given type, and several classes.&lt;/p&gt;

&lt;h1&gt;
  
  
  Base component
&lt;/h1&gt;

&lt;p&gt;The base component may contain any common logic that you like to share between different classes. This way, you will avoid code replication and will have the ability to enrich your classes by modifying code in a single place.&lt;/p&gt;

&lt;p&gt;Let's take a look at the class definition:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * - this class is providing base functionality for components.
 * - extend components with this class to get basic functionality.
 * - it is strongly advised to provide a type data &amp;lt;R&amp;gt; for this class 
 * to have an ability to provide the strictly typed data 
 * for the derived component before display it.
 */&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;BaseComponent&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;R&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="cm"&gt;/**
   * - the optional external data of extendable type
   */&lt;/span&gt;
  &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;R&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;The above example was simplified. But you can add, for example, an ID, name, or any other metadata.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Component interface
&lt;/h1&gt;

&lt;p&gt;Next, we will define an interface that will describe data for the factory. As you can see below, the only mandatory field is the type of component to be created. The component's data is optional. Later on, you can extend this interface to fit your needs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * component description
 */&lt;/span&gt;
&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;IComponent&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;BaseComponent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;R&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="cm"&gt;/** the component class type */&lt;/span&gt;
  &lt;span class="na"&gt;component&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="cm"&gt;/** the user defined extendable optional data */&lt;/span&gt;
  &lt;span class="nl"&gt;data&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;R&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Please, take a look at the definition of the component. First of all, we explicitly telling TypeScript that it should be derived from the BaseComponent class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;BaseComponent&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Next, we instructing TypeScript that the passed value has a constructor:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;component&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  Component data type
&lt;/h1&gt;

&lt;p&gt;It is impossible to get a type of data directly. But it is possible if we will leverage a helper type. Here it goes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * helper type which only meaning is to extract user-provided type from provided component
 */&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;ComponentData&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;W&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;W&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;BaseComponent&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;infer&lt;/span&gt; &lt;span class="nx"&gt;R&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;R&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;W&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To be short, this type tries to infer the user-defined type. If it is not possible when it will return the initial or unknown type. In either case, this will not produce an error.&lt;/p&gt;

&lt;h1&gt;
  
  
  Factory function
&lt;/h1&gt;

&lt;p&gt;The goal is to create a new instance of the desired component and return it only after the constructor will finish its work. Thus, by wrapping the logic into a promise, we can control what to do next in any situation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In case if the factory will successfully create the component, we can use it further. &lt;/li&gt;
&lt;li&gt;In other cases, we can notify other components of what had happened.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * create component
 * @param settings component settings
 */&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;create&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;BaseComponent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;R&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;ComponentData&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;settings&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;IComponent&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;R&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;settings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;component&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;settings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;The above example is simplified to catch the idea. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here is how we use the ComponentData type to infer the user-defined type. Firstly, we tell TypeScript that the component extends the BaseComponent class. After, we are giving a clue about where to search the user-defined type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;BaseComponent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;R&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;ComponentData&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Finally, we leverage the IComponent interface to get the proper type of data:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;settings&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;IComponent&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;R&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, as all is set, we can create the component, assign the passed data, and return the new instance to the caller.&lt;/p&gt;

&lt;h1&gt;
  
  
  Example
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;I'm living in the types-safe world of TypeScript for a pretty long time. So, I'm used to using it for frontend projects as well as backend code. That is why I prefer to describe all of the structures I'm working with via interfaces. And this example is not an exclusion.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's use all of that we have created above. To do so, we will create an interface and a class. Here is how they look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;IUser&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;BaseComponent&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;IUser&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And use it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;create&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;component&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;alice&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;TypeScript will check fields that we will pass to the data object. If a set of fields is wrong or types of a provided values mismatch, it will lead to a compilation error.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YTYjTcoE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/oxwvn50knw1tw0jcv5qs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YTYjTcoE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/oxwvn50knw1tw0jcv5qs.png" alt="Alt Text" title="Compilation Error"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Playground
&lt;/h1&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/ts-type-inference-fevd5"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h1&gt;
  
  
  Bottom line
&lt;/h1&gt;

&lt;p&gt;TypeScript allows you to infer the type of data from base classes. Thus, it is possible to use and create objects via factories. That, in turn, will add an extra layer of safety to your application. Also, it enables you to reduce code replication by moving common logic into a single place. This approach works for frontend as well as for backend: I'm using it for modal windows, for example.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/peacefullatom"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zQj764Ae--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Keep moving forward!</title>
      <dc:creator>Yuriy Markov</dc:creator>
      <pubDate>Fri, 31 Jan 2020 11:13:01 +0000</pubDate>
      <link>https://forem.com/peacefullatom/keep-moving-forward-3bpc</link>
      <guid>https://forem.com/peacefullatom/keep-moving-forward-3bpc</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Once a man told me:&lt;br&gt;
Give me $100, and I will thank you.&lt;br&gt;
Teach me how to earn $100, and I will be your friend.&lt;br&gt;
Share your success with me, and I will support you forever.&lt;br&gt;
So, share your success with your friends to get their support and spread your knowledge everywhere.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Support plays a great role in our social lives. It can be a support of any kind: you can share your knowledge, you can teach people, you can take part in someone's life.&lt;/p&gt;

&lt;p&gt;The number of ways you can support others is innumerable.&lt;/p&gt;

&lt;p&gt;I'm living in a world of IT, and I can share about how you can support others:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can contribute to the OSS projects. Each contribution matters.&lt;/li&gt;
&lt;li&gt;You can share your insights, productivity hacks, newly obtained knowledge.&lt;/li&gt;
&lt;li&gt;You can make your project an OSS. Thus, you will help others to save their time.&lt;/li&gt;
&lt;li&gt;You can blog about new features, frameworks, libraries, approaches, etc. Spreading the knowledge will help you to solidify your skills.&lt;/li&gt;
&lt;li&gt;You can become an advocate in your area of expertise.&lt;/li&gt;
&lt;li&gt;You can help by promoting tools/projects which you like and use daily.&lt;/li&gt;
&lt;li&gt;You can run a podcast, stream your knowledge using a platform of your choice.&lt;/li&gt;
&lt;li&gt;You can create/edit documentation for OSS projects. This work is very important and done very poor often.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Moreover, any of the listed types of support will be to your benefit. By supporting others, you will create your portfolio and your legacy that will help you to get a job or to start your project!&lt;/p&gt;

&lt;p&gt;In the age of the remote revolution, your online profile is one of the best assets you can create.&lt;/p&gt;

&lt;p&gt;And that is awesome! You will achieve these goals by keeping your online profile outstanding and crisp:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You will obtain new knowledge. The more you know, the more freedom you have.&lt;/li&gt;
&lt;li&gt;You will meet new people. You never know who of your peers can boost your career.&lt;/li&gt;
&lt;li&gt;You can earn by supporting others. An explosive combination: learn and earn!&lt;/li&gt;
&lt;li&gt;You will build the world you want to live in. "There's no fate but what we make" (c) The Terminator.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I hope that this post will help people to keep their heads up high and keep on going to their dreams, goals, whatever.&lt;/p&gt;

&lt;p&gt;Who knows? Maybe your dream is also a dream of someone else. And by turning dreams into reality, you will support other people as well!&lt;/p&gt;

&lt;p&gt;Thank you!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/peacefullatom"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zQj764Ae--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>motivation</category>
    </item>
    <item>
      <title>Customization of a blog created with Stackbit, Netlify, and dev.to</title>
      <dc:creator>Yuriy Markov</dc:creator>
      <pubDate>Wed, 29 Jan 2020 07:40:01 +0000</pubDate>
      <link>https://forem.com/peacefullatom/customizing-of-a-blog-created-with-stackbit-netlify-and-dev-to-4gmh</link>
      <guid>https://forem.com/peacefullatom/customizing-of-a-blog-created-with-stackbit-netlify-and-dev-to-4gmh</guid>
      <description>&lt;p&gt;Recently I created my blog using &lt;a href="https://scipios.netlify.com/posts/start-your-blog-in-10-minutes-3bjm/"&gt;a feature provided by dev.to&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Here are several tweaks you can do to adjust the blog to your needs.&lt;/p&gt;

&lt;h1&gt;
  
  
  Set up the domain name.
&lt;/h1&gt;

&lt;p&gt;After Stackbit finishes creating your site, Netlify will assign a random domain name. In my case, it was:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://wonderful-saturn-e6a47.netlify.com/"&gt;https://wonderful-saturn-e6a47.netlify.com/&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The link above is no longer available.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To set the desired subdomain name open the &lt;a href="https://app.netlify.com/sites/scipios/settings/general#site-information"&gt;Site information&lt;/a&gt; section:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--S1rhEuxz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/h42prg75six7xga2d61j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--S1rhEuxz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/h42prg75six7xga2d61j.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And tap the &lt;em&gt;Change site name&lt;/em&gt; button.&lt;/p&gt;

&lt;p&gt;Now type your desired subdomain name in the field &lt;em&gt;Site name&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lWlO7YTl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/33vym910fcbi2fg66egg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lWlO7YTl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/33vym910fcbi2fg66egg.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Alternatively, if you have a personal domain name, you can set a &lt;a href="https://app.netlify.com/sites/scipios/settings/domain#domains"&gt;custom domain&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bV6XkQK5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/khlqt2idhzt3swgfpuyn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bV6XkQK5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/khlqt2idhzt3swgfpuyn.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Tap &lt;em&gt;Add custom domain&lt;/em&gt; and follow the instructions.&lt;/p&gt;

&lt;h1&gt;
  
  
  Add Google Analytics
&lt;/h1&gt;

&lt;p&gt;This tweak requires a bit of technical work.&lt;/p&gt;

&lt;p&gt;First of all, clone your repository. To do so, go to your Github page and navigate to the repository created by Netlify. In my case, it looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zWBA0Wrz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/xx2kyn11vkijro481vcq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zWBA0Wrz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/xx2kyn11vkijro481vcq.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, click &lt;em&gt;Clone or download&lt;/em&gt; button and tap the &lt;em&gt;copy-to-clipboard&lt;/em&gt; icon.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ncPSMaXT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/i4ex82z7dlqwn4eu6sid.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ncPSMaXT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/i4ex82z7dlqwn4eu6sid.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After, open a terminal, type in "git clone" and paste the copied link. It will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/user/repository-name.git
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;If you are new to the IT world, you can read my article &lt;a href="https://scipios.netlify.com/posts/prerequisites-om5/"&gt;Prerequisites&lt;/a&gt;. You need to install git, NodeJs, and an IDE.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Great! Now open a cloned repository in IDE of your choice.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In the examples below, I'm using the &lt;a href="https://code.visualstudio.com/"&gt;Visual Studio Code&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;After cloning a repository, you need to install dependencies. To add them to your local repo go to built-in terminal and key in this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;You can refer to the list of shortcuts for the Visual Studio Code in this post &lt;a href="https://scipios.netlify.com/posts/productivity-boost-with-the-keyboard-in-visual-studio-code-35of/"&gt;Productivity boost with the keyboard in Visual Studio Code&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Next, to add the Google Analytics functionality, you need to install a respective plugin. So, open built-in terminal and type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save&lt;/span&gt; gatsby-plugin-google-analytics
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;After NPM finishes its work, you need to modify the &lt;em&gt;gatsby-config.js&lt;/em&gt; file. Press &lt;em&gt;Ctrl + P&lt;/em&gt; and start typing &lt;strong&gt;gatsby-config&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hljQvdVH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/hfalyj5u7wq6555qhzic.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hljQvdVH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/hfalyj5u7wq6555qhzic.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The configuration file will look like this (an excerpt):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;pathPrefix&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;siteMetadata&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./site-metadata.json&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;`gatsby-plugin-react-helmet`&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, let's update the plugins section:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;pathPrefix&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;siteMetadata&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./site-metadata.json&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s2"&gt;`gatsby-plugin-react-helmet`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`gatsby-plugin-google-analytics`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;options&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;trackingId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;UA-XXXXXXXXX-X&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Insert your Google Analytics id instead of &lt;em&gt;UA-XXXXXXXXX-X&lt;/em&gt; and save the file.&lt;/p&gt;

&lt;p&gt;Lastly, return to the built-in terminal and instruct git to push your updates to the remote:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;git add &lt;span class="nt"&gt;--all&lt;/span&gt;
git commit &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s2"&gt;"added google analytics"&lt;/span&gt;
git push
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Overview of commands:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;marks all of the altered files as ready to commit. &lt;/li&gt;
&lt;li&gt;commit your changes to the local repository and add a commit message. &lt;/li&gt;
&lt;li&gt;push your changes to the Github repository.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The Netlify will track any changes to the repository on Github and will compile and deploy your project right away!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The original steps are available at the &lt;a href="https://www.gatsbyjs.org/docs/adding-analytics/"&gt;GatsbyJs site&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's it! Now you can go to Google Analytics panel and track down your visitors.&lt;/p&gt;

&lt;p&gt;Happy blogging!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/peacefullatom"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zQj764Ae--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>blog</category>
    </item>
    <item>
      <title>Start your blog in 10 minutes!</title>
      <dc:creator>Yuriy Markov</dc:creator>
      <pubDate>Mon, 27 Jan 2020 07:18:22 +0000</pubDate>
      <link>https://forem.com/peacefullatom/start-your-blog-in-10-minutes-3bjm</link>
      <guid>https://forem.com/peacefullatom/start-your-blog-in-10-minutes-3bjm</guid>
      <description>&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/peacefullatom"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zQj764Ae--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Recently I discovered that it is possible to build a blog from posts on the &lt;a href="https://dev.to"&gt;dev.to&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Isn't it awesome?&lt;/p&gt;

&lt;p&gt;To make it happen, you need to have an account at &lt;a href="https://github.com"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Once you &lt;a href="https://github.com/join"&gt;create&lt;/a&gt; an account, you can sign in to the &lt;a href="https://dev.to/users/auth/github"&gt;dev.to&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Next, you need to go to &lt;em&gt;Settings&lt;/em&gt; - &lt;em&gt;Integrations&lt;/em&gt; and tap &lt;a href="https://app.stackbit.com/create?ref=devto"&gt;Create New Stackbit Site&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Once you on the Stackbit's start page, it takes four steps to create &lt;strong&gt;your&lt;/strong&gt; blog:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;select a theme
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1WIWZ9bj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/c4ysvbi83o83hhgs72vq.png" alt="Alt Text"&gt;
&lt;/li&gt;
&lt;li&gt;choose a site generator
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2ALEOWoy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/5vimi766az9qsu74rmpo.png" alt="Alt Text"&gt;
&lt;/li&gt;
&lt;li&gt;connect to your dev.to account
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_n8YQxDx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/7oolza73f4ce8mdn55y8.png" alt="Alt Text"&gt;
&lt;/li&gt;
&lt;li&gt;deploy your blog
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jLlbE5Kn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/os035sr5xw26dex6kvuy.png" alt="Alt Text"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;If you don't have a &lt;a href="https://www.netlify.com/"&gt;Netlify&lt;/a&gt; account, then it will take several minutes to sign up using the Github account.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As a result, you will have a new repository in your Github account.&lt;/p&gt;

&lt;p&gt;Next, you need to correct the default data from the start page.&lt;/p&gt;

&lt;p&gt;To do so, go to your Github repository then navigate to &lt;em&gt;src/pages/index.md&lt;/em&gt;.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aNigGbSb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/l9ekn76277aoff2isup8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aNigGbSb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/l9ekn76277aoff2isup8.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
Once you are where click the edit button and let your fantasy fly a bit around to write something about yourself!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://scipios.netlify.com/posts/start-your-blog-in-10-minutes-3bjm/"&gt;You can check out this post on my blog&lt;/a&gt;!&lt;/p&gt;

&lt;p&gt;That's it! Happy blogging!&lt;/p&gt;

</description>
      <category>blog</category>
    </item>
    <item>
      <title>Can you improve your creativity?</title>
      <dc:creator>Yuriy Markov</dc:creator>
      <pubDate>Thu, 23 Jan 2020 11:33:34 +0000</pubDate>
      <link>https://forem.com/peacefullatom/can-you-improve-your-creativity-37pn</link>
      <guid>https://forem.com/peacefullatom/can-you-improve-your-creativity-37pn</guid>
      <description>&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/peacefullatom"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zQj764Ae--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Definition
&lt;/h1&gt;

&lt;p&gt;People are different - that's the fact.&lt;/p&gt;

&lt;p&gt;Some of us are super creative since birth.&lt;/p&gt;

&lt;p&gt;Others (falsely) believe that they are not able to be creative.&lt;/p&gt;

&lt;p&gt;But first things first.&lt;/p&gt;

&lt;p&gt;Let's have a look at the &lt;a href="https://en.wikipedia.org/wiki/Creativity"&gt;definition&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;Creativity is a phenomenon whereby something new and somehow valuable is formed. The created item may be intangible (such as an idea, a scientific theory, a musical composition, or a joke) or a physical object (such as an invention, a literary work, or a painting).&lt;/p&gt;

&lt;h1&gt;
  
  
  Experiment
&lt;/h1&gt;

&lt;p&gt;Now ask yourself - are you able to create something new?&lt;/p&gt;

&lt;p&gt;The answer is obvious - yes!&lt;/p&gt;

&lt;p&gt;If you don't agree with me, then take a pen and paper (or something similar, even sand and a stick is a good option). Now draw a line.&lt;/p&gt;

&lt;p&gt;Voila! You have created something new! And it was simple.&lt;/p&gt;

&lt;p&gt;But you can contr argue: wasn't that too simple? what about something sophisticated? something beautiful? something highly technological?&lt;/p&gt;

&lt;p&gt;Let's have a look at the line we have drawn recently.&lt;/p&gt;

&lt;p&gt;Now imagine any painted masterpiece you like.&lt;/p&gt;

&lt;p&gt;Initially, it was the line just like yours! So, anything that looks impossible for you is starting like this - from something incredibly simple!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Big things have small beginnings&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Recipe
&lt;/h1&gt;

&lt;p&gt;Now I hope that you agreed with me, and let's understand how creativity can be trained.&lt;/p&gt;

&lt;p&gt;Here is my recipe:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;be persistent&lt;/li&gt;
&lt;li&gt;don't set goals, create habits&lt;/li&gt;
&lt;li&gt;always learn&lt;/li&gt;
&lt;li&gt;don't be afraid of failures&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;As a side note, I want to mention that all of these points are connected. And even are parts of each other.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Be persistent
&lt;/h1&gt;

&lt;p&gt;To be persistent means for me to keep doing some activity. So, if you've tried something once and failed, it doesn't mean that you should stop.&lt;/p&gt;

&lt;p&gt;In my case, it took about 7 years before I've created my first solo project. It was an interface for the &lt;a href="https://en.wikipedia.org/wiki/Set-top_box"&gt;Set-top box&lt;/a&gt;. It was interesting: the user had nothing but the remote control. But the users were able to see tv guides, horoscopes, weather forecasts, play games, record shows, schedule time to turn on or off the TV, check their balance, etc.&lt;/p&gt;

&lt;h1&gt;
  
  
  Don't set goals, create habits
&lt;/h1&gt;

&lt;p&gt;Pretty early in my life, I've discovered that it is hard to follow multiple goals. But it is very easy to create habits which will lead you to your goals.&lt;/p&gt;

&lt;p&gt;There were times when I wanted:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a car&lt;/li&gt;
&lt;li&gt;an apartment&lt;/li&gt;
&lt;li&gt;travel the world&lt;/li&gt;
&lt;li&gt;become a developer&lt;/li&gt;
&lt;li&gt;lead a healthy way of life&lt;/li&gt;
&lt;li&gt;learn to speak English&lt;/li&gt;
&lt;li&gt;a countryside house&lt;/li&gt;
&lt;li&gt;get married&lt;/li&gt;
&lt;li&gt;etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And that's only the top of the list!&lt;/p&gt;

&lt;p&gt;By looking at that list, you can easily separate it in two categories:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;exact result (e.g., an apartment)&lt;/li&gt;
&lt;li&gt;vague result (e.g., become a developer)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the case of exact results, you can say when the goal is achieved: I have documents that tell that I'm the owner now.&lt;/p&gt;

&lt;p&gt;But what about my dream about becoming a developer?&lt;/p&gt;

&lt;p&gt;Ok. I wrote 10 lines of code, and they are even working! Am I a developer already?&lt;/p&gt;

&lt;p&gt;So, it is much easier to create habits:&lt;br&gt;
Want to buy something expensive? Set aside 10% of your income. &lt;br&gt;
Want to be a developer? Find a junior position. &lt;br&gt;
Want to learn English? Practice every day for one hour.&lt;/p&gt;

&lt;p&gt;This way, you'll keep on going to your dreams with ease.&lt;/p&gt;

&lt;h1&gt;
  
  
  Always learn
&lt;/h1&gt;

&lt;p&gt;You will never know what knowledge may become handy in the future. The answer is simple - always learn.&lt;/p&gt;

&lt;p&gt;The real-life example. Steve Jobs followed a class in calligraphy. It inspired him to add beautiful typography to the first Mac.&lt;/p&gt;

&lt;p&gt;But remember - be persistent. Dive deeper than: "I read about this topic a few years ago". To solidify your knowledge, start to use it.&lt;/p&gt;

&lt;h1&gt;
  
  
  Don't be afraid of failures
&lt;/h1&gt;

&lt;p&gt;The failure is your friend. And it is so: you should use your failures as a source of knowledge.&lt;/p&gt;

&lt;p&gt;Another personal example: I've passed the exam and obtained a driver's license only from the third attempt!&lt;/p&gt;

&lt;p&gt;Yes, I've failed two times in a row. But every time I've failed, I've returned to the driver's school and trained again. Today I am a driver with more than ten years of experience.&lt;/p&gt;

&lt;h1&gt;
  
  
  Outcomes
&lt;/h1&gt;

&lt;p&gt;At this point in my story, my oldest son asked: "That's cool! But how will this help me improve my creativity?".&lt;/p&gt;

&lt;p&gt;Well, creativity is a skill as any other skill that you have.&lt;/p&gt;

&lt;p&gt;So, to improve creativity - start to create things!&lt;/p&gt;

&lt;p&gt;Yes, it's all that simple. And all of the above will help you in your efforts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;be persistent in your efforts&lt;/li&gt;
&lt;li&gt;create habits that will lead you to your goals&lt;/li&gt;
&lt;li&gt;always learn new stuff: the more you know, the more freedom you have&lt;/li&gt;
&lt;li&gt;don't be afraid to fail, be afraid to not to try&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>productivity</category>
    </item>
    <item>
      <title>How many hours a day do you think you work?</title>
      <dc:creator>Yuriy Markov</dc:creator>
      <pubDate>Wed, 22 Jan 2020 09:18:19 +0000</pubDate>
      <link>https://forem.com/peacefullatom/how-many-hours-a-day-do-you-think-you-work-583e</link>
      <guid>https://forem.com/peacefullatom/how-many-hours-a-day-do-you-think-you-work-583e</guid>
      <description>&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/peacefullatom"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zQj764Ae--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;People usually think that they are working some exact number of time.&lt;/p&gt;

&lt;p&gt;But that is not how our brain works.&lt;/p&gt;

&lt;p&gt;Once the brain has a task, it will keep allocating part of its computing time to find the solution until it will complete the job.&lt;/p&gt;

&lt;p&gt;Though it is also not the end of the story: after the brain will find the solution, it will spend some time to memorize the results.&lt;/p&gt;

&lt;p&gt;Now, let's take a look at this simple timeline:&lt;/p&gt;

&lt;p&gt;9 am - new task, complexity - inhuman!&lt;br&gt;
10 am - meetup&lt;br&gt;
11 am - read emails, etc.&lt;br&gt;
Noon - hey it's lunchtime!&lt;br&gt;
1 pm - concentrating on a task&lt;br&gt;
2 pm - help request from your colleagues&lt;br&gt;
3 pm - your boss wants to see you&lt;br&gt;
4 pm - concentrating on a task&lt;br&gt;
5 pm - coffee time!&lt;br&gt;
6 pm - oops! time to go home!&lt;br&gt;
7 pm - traffic jams&lt;br&gt;
8 pm - dinner&lt;br&gt;
9 pm - family time&lt;br&gt;
10 pm - time to sleep&lt;br&gt;
11 pm - the solution!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note that the above timeline is very simplified to catch the idea.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So, why the brain finds the solution even if you are busy with some different tasks?&lt;/p&gt;

&lt;p&gt;The answer is simple - the brain was solving the problem all this time. But your higher level of consciousness was not aware of this work.&lt;/p&gt;

&lt;p&gt;How is this possible?&lt;/p&gt;

&lt;p&gt;Let's think about the brain as of the superset of neural networks. Every part of this superset is also a set of neural networks.&lt;/p&gt;

&lt;p&gt;Can you imagine the complexity of this structure? Also, it is a genuine miracle that this superstructure is placed inside our skull!&lt;/p&gt;

&lt;p&gt;Moreover, this super network always works. When you eat, walk, entertain, sleep, even if you are unconsciousness.&lt;/p&gt;

&lt;p&gt;Another noticeable thing that all these networks are working independently. And that allows our brain to be a true multitasker!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Having all of the above in mind, can you imagine the overall computing power of our brain?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The general simplified scheme of working of our built-in neural network looks like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;get the task&lt;/li&gt;
&lt;li&gt;divide the task into the chunks&lt;/li&gt;
&lt;li&gt;assign each chunk to some part of the network&lt;/li&gt;
&lt;li&gt;memorize the result after all of the assigned networks report back&lt;/li&gt;
&lt;li&gt;after the result is memorized start notifying higher-level network&lt;/li&gt;
&lt;li&gt;wait for a time-slot to report that the task is finished&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thanks to this approach, all of us have those "a-ha" moments. &lt;br&gt;
So, it is wrong to think that you are only working when you are in the workplace.&lt;/p&gt;

&lt;p&gt;Depending on your overall experience and the complexity of a task, your brain will allocate as much time as it needs to find the solution.&lt;/p&gt;

&lt;p&gt;So, the right answer is that your brain is always working, and so do you.&lt;/p&gt;

</description>
      <category>productivity</category>
    </item>
    <item>
      <title>Is it practical to use blockchain to track resource distribution? #discussion</title>
      <dc:creator>Yuriy Markov</dc:creator>
      <pubDate>Wed, 22 Jan 2020 07:23:08 +0000</pubDate>
      <link>https://forem.com/peacefullatom/is-it-practical-to-use-blockchain-to-track-resource-distribution-discussion-3k10</link>
      <guid>https://forem.com/peacefullatom/is-it-practical-to-use-blockchain-to-track-resource-distribution-discussion-3k10</guid>
      <description>&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/peacefullatom"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zQj764Ae--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Is it practical to use blockchain to track resource distribution?&lt;br&gt;
For example. The city provides the water to different consumers and tracks how much water was consumed by each consumer via IoT using the blockchain.&lt;br&gt;
I'm exploring a new area of knowledge and have lots of dumb questions.&lt;br&gt;
I know that the IT and financial worlds are leveraging the blockchain technology to their benefits.&lt;br&gt;
But what about other areas of human activities?&lt;br&gt;
It would be great to know your opinion.&lt;/p&gt;

</description>
      <category>iot</category>
      <category>blockchain</category>
    </item>
    <item>
      <title>Why we should be a better version of ourselves</title>
      <dc:creator>Yuriy Markov</dc:creator>
      <pubDate>Tue, 21 Jan 2020 08:58:01 +0000</pubDate>
      <link>https://forem.com/peacefullatom/why-we-should-be-a-better-version-of-ourselves-b9a</link>
      <guid>https://forem.com/peacefullatom/why-we-should-be-a-better-version-of-ourselves-b9a</guid>
      <description>&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/peacefullatom"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zQj764Ae--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After years of thinking, experimentation, and observation, I came to some conclusions.&lt;/p&gt;

&lt;p&gt;Here is one of them:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Any human activity is a way to express yourself.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But on the other hand:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Any human activity can be described as a process. And any process can be improved.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now let's combine these two ideas:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Any human activity is a process of expressing yourself that can be continuously improved.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So, the point is that we are changing the world by expressing ourselves, and that can be improved.&lt;/p&gt;

&lt;p&gt;At this point, my interlocutor usually asks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Why should I improve myself?&lt;/li&gt;
&lt;li&gt;Why should I change the ways I'm expressing myself?&lt;/li&gt;
&lt;li&gt;Why do you think that I'm changing the world?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Or something similar.&lt;/p&gt;

&lt;p&gt;And, again, here is my point of view:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;For me, one of the ways of self-expression is to write code.&lt;/li&gt;
&lt;li&gt;If I'm good at expressing myself, I also have these perks:&lt;/li&gt;
&lt;li&gt;I'm producing fast a high-quality code&lt;/li&gt;
&lt;li&gt;I have a bigger salary&lt;/li&gt;
&lt;li&gt;I can afford whatever I want&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I hope that this will help to improve somebody's world.&lt;/p&gt;

</description>
      <category>watercooler</category>
    </item>
    <item>
      <title>Why the distributed company is a viable option?</title>
      <dc:creator>Yuriy Markov</dc:creator>
      <pubDate>Mon, 20 Jan 2020 11:23:32 +0000</pubDate>
      <link>https://forem.com/peacefullatom/why-the-distributed-company-is-a-viable-option-289</link>
      <guid>https://forem.com/peacefullatom/why-the-distributed-company-is-a-viable-option-289</guid>
      <description>&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/peacefullatom"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zQj764Ae--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Remotely managed companies nowadays have benefits.&lt;/p&gt;

&lt;p&gt;As an employer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;you don't need an office&lt;/li&gt;
&lt;li&gt;you don't need hardware for your employees&lt;/li&gt;
&lt;li&gt;you don't have to select employees only from the local area&lt;/li&gt;
&lt;li&gt;your employees are already equipped with hardware&lt;/li&gt;
&lt;li&gt;you don't need to pay for bandwidth enough for comfortable work of many people&lt;/li&gt;
&lt;li&gt;you don't need to buy fancy stuff for an office, e.g. ping-pong table, watercooler, etc.&lt;/li&gt;
&lt;li&gt;you need less money to start your business&lt;/li&gt;
&lt;li&gt;you can afford to run several businesses&lt;/li&gt;
&lt;li&gt;you can travel and keep growing your business&lt;/li&gt;
&lt;li&gt;you can decrease your carbon footprint to zero!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As an employee:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;you can work with any company in the world&lt;/li&gt;
&lt;li&gt;you don't need to spend time to commute&lt;/li&gt;
&lt;li&gt;you can organize your workspace to your needs&lt;/li&gt;
&lt;li&gt;you can benefit from online services such as codesandbox.io to start working at no cost&lt;/li&gt;
&lt;li&gt;you do not need to bother about dress-code&lt;/li&gt;
&lt;li&gt;you can spend more time with your relatives and beloved&lt;/li&gt;
&lt;li&gt;you will eat healthy food of your choice&lt;/li&gt;
&lt;li&gt;you can work out as many times a day as you want&lt;/li&gt;
&lt;li&gt;you choose the time to work - no more 9 to 5 schedule&lt;/li&gt;
&lt;li&gt;you can attend conferences and keep working&lt;/li&gt;
&lt;li&gt;no problem with scheduling a visit to a doctor, advocate, etc.&lt;/li&gt;
&lt;li&gt;you can decrease your carbon footprint to zero!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are the benefits of remote working.&lt;/p&gt;

&lt;p&gt;The only thing that you should choose wisely the software stack that will allow you to achieve all of these advantages!&lt;/p&gt;

&lt;p&gt;And which advantages in remote lifestyle do you know/fond of?&lt;/p&gt;

</description>
      <category>remote</category>
    </item>
    <item>
      <title>The lifestyle of the digital nomad</title>
      <dc:creator>Yuriy Markov</dc:creator>
      <pubDate>Mon, 20 Jan 2020 07:49:04 +0000</pubDate>
      <link>https://forem.com/peacefullatom/the-lifestyle-of-the-digital-nomad-507g</link>
      <guid>https://forem.com/peacefullatom/the-lifestyle-of-the-digital-nomad-507g</guid>
      <description>&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/peacefullatom"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zQj764Ae--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Among many perks of a remote lifestyle, there is one that fits many - you can be a digital nomad.&lt;/p&gt;

&lt;p&gt;If you are a part of a distributed team and have a dream to explore the world, then it is an awesome combination!&lt;/p&gt;

&lt;p&gt;This set of opportunities will allow you to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Seek inspiration.&lt;/li&gt;
&lt;li&gt;Meet new people.&lt;/li&gt;
&lt;li&gt;Learn new languages.&lt;/li&gt;
&lt;li&gt;Attend conferences.&lt;/li&gt;
&lt;li&gt;Explore the world!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And it doesn't matter which is your role in the team: chief, developer, public relations, designer, representative of any kind, etc.&lt;/p&gt;

&lt;p&gt;If your activities are bound to some sophisticated set of equipment that you're unable to carry around, you still can spend part of your time as a nomad.&lt;/p&gt;

&lt;p&gt;And it also will include everything from the above list.&lt;/p&gt;

&lt;p&gt;The important thing is that your time is under your control.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Explore the world - express yourself!&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>remote</category>
    </item>
    <item>
      <title>The 2020s is the decade of remote transformation</title>
      <dc:creator>Yuriy Markov</dc:creator>
      <pubDate>Mon, 20 Jan 2020 06:44:35 +0000</pubDate>
      <link>https://forem.com/peacefullatom/the-2020s-is-the-decade-of-remote-transformation-n25</link>
      <guid>https://forem.com/peacefullatom/the-2020s-is-the-decade-of-remote-transformation-n25</guid>
      <description>&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/peacefullatom"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zQj764Ae--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The current decade is a time of explosive growth of a remote lifestyle.&lt;/p&gt;

&lt;p&gt;Why?&lt;/p&gt;

&lt;p&gt;These changes are upcoming thanks to world-wide-web that has reached every corner of our home planet and stretches to the orbit of the Moon.&lt;/p&gt;

&lt;p&gt;Even Mars is nowadays is a part of the global web, though it is not yet publicly accessible.&lt;/p&gt;

&lt;p&gt;The trick is that the web is not only the way to communicate.&lt;/p&gt;

&lt;p&gt;It also allows you to share your goods, service, or content and to transfer money.&lt;/p&gt;

&lt;p&gt;It is also important that it is irrelevant what kind of values are you offering - real-world or digital.&lt;/p&gt;

&lt;p&gt;The modern logistics is a wonder of speed and quality.&lt;/p&gt;

&lt;p&gt;Let's sum it up:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;you can reach any person on this planet&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;you can deliver whatever you able to produce&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;you can receive money as a reward to your efforts&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Doesn't it sound very attractive?&lt;/p&gt;

&lt;p&gt;For me, it sounds very promising!&lt;/p&gt;

&lt;p&gt;What is your opinion?&lt;/p&gt;

</description>
      <category>remote</category>
    </item>
    <item>
      <title>Having fun with TypeScript!</title>
      <dc:creator>Yuriy Markov</dc:creator>
      <pubDate>Wed, 15 Jan 2020 16:16:17 +0000</pubDate>
      <link>https://forem.com/peacefullatom/having-fun-with-typescript-29b0</link>
      <guid>https://forem.com/peacefullatom/having-fun-with-typescript-29b0</guid>
      <description>&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/peacefullatom" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.buymeacoffee.com%2Fbuttons%2Fdefault-orange.png" alt="Buy Me A Coffee"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Table Of Contents
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Conventions&lt;/li&gt;
&lt;li&gt;Button types&lt;/li&gt;
&lt;li&gt;Discriminated unions&lt;/li&gt;
&lt;li&gt;Intermediate interfaces&lt;/li&gt;
&lt;li&gt;Working types&lt;/li&gt;
&lt;li&gt;Example&lt;/li&gt;
&lt;li&gt;Code sandbox&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;TypeScript makes developers' life easier.&lt;/p&gt;

&lt;p&gt;In this article, we will walk through the creation of a set of types and interfaces.&lt;/p&gt;

&lt;p&gt;The playground for this example is attached at the end of this article.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conventions
&lt;/h1&gt;

&lt;p&gt;In my practice, I prefer to use types to define unions. The role of interfaces is to describe an object structure.&lt;/p&gt;

&lt;p&gt;Why?&lt;/p&gt;

&lt;p&gt;Thus, it is easier to distinguish types and interfaces.&lt;/p&gt;

&lt;p&gt;Here is an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/** a function that returns a generic type */&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;GenericFunction&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="cm"&gt;/**
 * a type that can be a generic value or
 * a function that returns a generic value
 */&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;GenericMixedValue&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;GenericFunction&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="cm"&gt;/** description of an element */&lt;/span&gt;
&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;IElement&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="cm"&gt;/** disabled state is of type GenericMixedValue */&lt;/span&gt;
  &lt;span class="nl"&gt;disabled&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;GenericMixedValue&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cm"&gt;/** a function that randomly disables an element :) */&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;disabled&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;GenericFunction&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mf"&gt;0.5&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="cm"&gt;/** declaration of an element */&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;IElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;disabled&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, all types and interfaces are declared in &lt;a href="https://en.wikipedia.org/wiki/Camel_case" rel="noopener noreferrer"&gt;CamelCase&lt;/a&gt;, but interfaces are prefixed with capital letter &lt;em&gt;i&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;This way, you will always know that the declaration refers to an interface:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;IElement&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the other hand, you will perfectly see when the type is referred:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;disabled&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;GenericFunction&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Button types
&lt;/h1&gt;

&lt;p&gt;In our example, we will define a flexible button description with the help of TypeScript.&lt;/p&gt;

&lt;p&gt;A button can be of any type from this set: &lt;em&gt;positive&lt;/em&gt;, &lt;em&gt;negative&lt;/em&gt;, &lt;em&gt;neutral&lt;/em&gt;, and &lt;em&gt;custom&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Here is the definition:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/** positive button type */&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;buttonTypePositive&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;positive&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="cm"&gt;/** negative button type */&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;buttonTypeNegative&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;negative&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="cm"&gt;/** neutral button type */&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;buttonTypeNeutral&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;neutral&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="cm"&gt;/** custom button type */&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;buttonTypeCustom&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;custom&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, let's define two types: &lt;em&gt;ButtonTypeMain&lt;/em&gt; and &lt;em&gt;ButtonType&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/** main button types */&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;ButtonTypeMain&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;buttonTypePositive&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;buttonTypeNegative&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="cm"&gt;/** all button types */&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;ButtonType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;buttonTypeNeutral&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;buttonTypeCustom&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;ButtonTypeMain&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;The &lt;em&gt;ButtonTypeMain&lt;/em&gt; will be used later to define a union type.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The &lt;em&gt;ButtonTypeMain&lt;/em&gt; set includes a &lt;em&gt;positive&lt;/em&gt; and &lt;em&gt;negative&lt;/em&gt; type of button, while the &lt;em&gt;ButtonTypeMain&lt;/em&gt; combines &lt;em&gt;all&lt;/em&gt; of the types.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;a href="http://www.typescriptlang.org/docs/handbook/advanced-types.html#discriminated-unions" rel="noopener noreferrer"&gt;Discriminated unions&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;Discriminated unions are the wonder of typing!&lt;/p&gt;

&lt;p&gt;With the help of such unions, you can gain flexibility via TypeScript!&lt;/p&gt;

&lt;p&gt;Let's look further at our example.&lt;/p&gt;

&lt;p&gt;Firstly, let's define a base interface &lt;em&gt;IButton&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/** base button interface */&lt;/span&gt;
&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;IButton&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ButtonType&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;title&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;disabled&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;style&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;action&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Please note that all of the interface parameters are optional except for the &lt;em&gt;type&lt;/em&gt;. Another important moment is that the &lt;em&gt;type&lt;/em&gt; parameter can accept only values from &lt;em&gt;ButtonType&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Next, we will use the &lt;em&gt;type&lt;/em&gt; parameter to declare the interfaces of exact button types.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/** positive button interface */&lt;/span&gt;
&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;IButtonPositive&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;IButton&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;buttonTypePositive&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;handleEnterKey&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ok, let's take a look at what has happened here a bit more thoroughly:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This interface extends and thus inherits properties of the &lt;em&gt;IButton&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;This interface sets the &lt;em&gt;type&lt;/em&gt; property to &lt;em&gt;buttonTypePositive&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;This interface adds new optional field &lt;em&gt;handleEnterKey&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, we have created the first discriminated interface.&lt;/p&gt;

&lt;p&gt;Next, let's add a discriminated interface for the negative button as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/** negative button interface */&lt;/span&gt;
&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;IButtonNegative&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;IButton&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;buttonTypeNegative&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;handleEscapeKey&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Its declaration looks similar to the declaration of the IButtonPositive interface with two major differences:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This interface sets the &lt;em&gt;type&lt;/em&gt; property to &lt;em&gt;buttonTypeNegative&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;This interface adds new optional field &lt;em&gt;handleEscapeKey&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To catch the difference by hands, let's create a union type that will make the discrimination work:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/** test union button type */&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;ButtonUnion&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;IButtonPositive&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;IButtonNegative&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's try to define a positive button. Take a look at the screenshot:&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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fnkw6fgfe0hv3zv1tf80m.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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fnkw6fgfe0hv3zv1tf80m.png" title="Define a positive button type" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;TypeScript allows us to define the &lt;em&gt;handleEnterKey&lt;/em&gt; field.&lt;/p&gt;

&lt;p&gt;But if we will change the value to "negative":&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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fr1g2tc82l75rvmzgw591.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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fr1g2tc82l75rvmzgw591.png" title="Define a negative button type" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;TypeScript will allow defining the &lt;em&gt;handleEscapeKey&lt;/em&gt; field!&lt;/p&gt;

&lt;p&gt;Moreover, if we will try to set a not appropriate field we will get an error:&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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fln0p3mqonaes57um28zr.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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fln0p3mqonaes57um28zr.png" title="handleEnterKey does not exist on positive type" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Conclusion: the discriminative types allow having a single flexible interface with the exact set of properties selected by the value of a field.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Intermediate interfaces
&lt;/h1&gt;

&lt;p&gt;Interfaces can be extended. But what if at some level you have to derive several interfaces that have a lot in common?&lt;/p&gt;

&lt;p&gt;In this case, we can leverage an intermediate interface.&lt;/p&gt;

&lt;p&gt;Let's take a look at the example below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;IElement&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;IInput&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;IElement&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;placeholder&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;tooltip&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;IStringInput&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;IInput&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;string&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;INumberInput&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;IInput&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;number&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;min&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;max&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;step&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, &lt;em&gt;IElement&lt;/em&gt; is a base interface that declares a generic &lt;em&gt;value&lt;/em&gt; and property &lt;em&gt;type&lt;/em&gt; to build discriminant interfaces.&lt;/p&gt;

&lt;p&gt;Next, we declare the &lt;em&gt;IInput&lt;/em&gt;, which acts as an intermediate interface. This interface adds a set of optional fields: &lt;em&gt;placeholder&lt;/em&gt; and &lt;em&gt;tooltip&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Lastly, we use the &lt;em&gt;IInput&lt;/em&gt; to build two data type-specific interfaces: &lt;em&gt;INumberInput&lt;/em&gt; and &lt;em&gt;IStringInput&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Now, let's use that knowledge to finish building interfaces for the button.&lt;/p&gt;

&lt;p&gt;Firstly, the intermediate interface:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/** extender interface for the neutral and custom buttons */&lt;/span&gt;
&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;IButtonCustomized&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;IButton&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;action&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;handleKey&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;em&gt;IButtonCustomized&lt;/em&gt; extends &lt;em&gt;IButton&lt;/em&gt; interface and adds a common optional property: &lt;em&gt;handleKey&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The important moment here is that this interface makes fields &lt;em&gt;title&lt;/em&gt; and &lt;em&gt;action&lt;/em&gt; &lt;strong&gt;mandatory&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Next, let's define the remaining interfaces:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/** neutral button interface */&lt;/span&gt;
&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;IButtonNeutral&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;IButtonCustomized&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;buttonTypeNeutral&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cm"&gt;/** custom button interface */&lt;/span&gt;
&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;IButtonCustom&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;IButtonCustomized&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;buttonTypeCustom&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;style&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ButtonStyle&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Working types
&lt;/h1&gt;

&lt;p&gt;To start using everything that we build let's define a union type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/** union button type */&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;ButtonUnion&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;ButtonTypeMain&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;IButtonPositive&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;IButtonNegative&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;IButtonNeutral&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;IButtonCustom&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then export the type that will be used by end-users:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/** button type */&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Button&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;GenericValueOrArray&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ButtonUnion&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Another side note: don't export intermediate and helping types/interfaces because they will confuse the users.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Example
&lt;/h1&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Button&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./types&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// defining the buttons&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;buttons&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Button&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;positive&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;negative&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;neutral&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I won't do anything!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;action&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;neutral hit&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;custom&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hit me!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;action&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;custom hit&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;custom&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Feel free to use the embedded playground at the bottom of the article to see the results.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;From the above declaration, you can figure out several things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Buttons list can be declared as a string, an object, or an array of strings/objects.&lt;/li&gt;
&lt;li&gt;Predefined button types &lt;em&gt;positive&lt;/em&gt; and &lt;em&gt;negative&lt;/em&gt; can be declared as a string (later on, the parser will add default values if something is missing).&lt;/li&gt;
&lt;li&gt;The value of the &lt;em&gt;type&lt;/em&gt; property modifies the list of available fields.&lt;/li&gt;
&lt;li&gt;TypeScript will throw an error if the user will try to define a field that is not defined in the respective interface.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Isn't that lovely?&lt;/p&gt;

&lt;p&gt;Here goes a simplified example of the parser:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Button&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./types&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="cm"&gt;/**
 * create the button
 * @param type button type
 * @param className button style
 * @param caption button title
 * @param action button action
 */&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;caption&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;ButtonAction&lt;/span&gt;
&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;HTMLButtonElement&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;button&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// implement your logic here&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cm"&gt;/**
 * parse buttons list and create elements
 * @param buttons buttons list
 */&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;buttons&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Button&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;HTMLButtonElement&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;HTMLButtonElement&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;buttons&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;string&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;buttons&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;buttons&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;buttons&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;string&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;object&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
          &lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;buttons&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;object&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;buttons&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;buttons&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;buttons&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;buttons&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;buttons&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Please, refer to the embedded code sandbox at the bottom of the post for a more solid example.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The &lt;em&gt;parse&lt;/em&gt; function checks the type of the passed data:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If this is a string, then we try to create a button with default settings.&lt;/li&gt;
&lt;li&gt;Otherwise, we iterate the array of strings/objects.&lt;/li&gt;
&lt;li&gt;Lastly, we received an object that has a &lt;em&gt;type&lt;/em&gt; field.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All the rest data will be skipped to avoid potential errors.&lt;/p&gt;

&lt;p&gt;If data fits any conditions listed above, then we try to create a button and push it into the resulting array.&lt;/p&gt;

&lt;h1&gt;
  
  
  Code sandbox
&lt;/h1&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/agitated-wiles-mf281"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;TypeScript extends your abilities to define data structure, as well as to manipulate them.&lt;/p&gt;

&lt;p&gt;Effectively combining the union types with sets of interfaces, we can forge the discriminated types that will add one more level of flexibility to your projects.&lt;/p&gt;

&lt;p&gt;Moreover, this will also improve the user experience of other fellow developers who will benefit via time-saving and productivity-boosting.&lt;/p&gt;

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