<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Vladyslav</title>
    <description>The latest articles on DEV Community by Vladyslav (@vbrdnk).</description>
    <link>https://dev.to/vbrdnk</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%2F431484%2Fdf9a7f2b-4cf2-4757-a930-0b8733e63552.jpg</url>
      <title>DEV Community: Vladyslav</title>
      <link>https://dev.to/vbrdnk</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vbrdnk"/>
    <language>en</language>
    <item>
      <title>Deep dive into TypeScript Utility Types. Part 1</title>
      <dc:creator>Vladyslav</dc:creator>
      <pubDate>Wed, 20 Jul 2022 15:41:06 +0000</pubDate>
      <link>https://dev.to/vbrdnk/deep-dive-into-typescript-utility-types-part-1-82</link>
      <guid>https://dev.to/vbrdnk/deep-dive-into-typescript-utility-types-part-1-82</guid>
      <description>&lt;p&gt;If you are using TypeScript on an everyday basis or just getting familiar with it, you might have heard about its utility types. Utility types are the built-in types that you can use as a developer without needing any additional packages. Most of the time we don't ponder about how those types are designed under the hood and just use them according to &lt;a href="https://www.typescriptlang.org/docs/"&gt;TypeScript Documentation&lt;/a&gt;. Don't take me wrong, there is nothing bad with that, especially considering that TypeScript is managed by Microsoft and there is a bunch of smart people responsible for it. But to better understand how utility types work, there is a better approach - to implement them on your own.&lt;/p&gt;

&lt;p&gt;So, let's dive deep into it and try to implement custom utility types.&lt;/p&gt;

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

&lt;p&gt;The first type we'll discuss today is &lt;code&gt;Partial&amp;lt;T&amp;gt;&lt;/code&gt;. According to documentation:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;Partial&amp;lt;T&amp;gt;&lt;/code&gt; constructs a type with all properties of &lt;code&gt;T&lt;/code&gt; set to optional and returns a type that represents all subsets of a given type. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Before implementing any of the TypeScript Utility types there is one important thing we should discuss - Mapped types. In short, Mapped types build on the syntax for index signatures, which are used to declare the types of properties which has not been declared ahead of time. Okay, less talking more coding.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--m8SOAbiF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v0rl2szuvpl9hwqbsnqa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--m8SOAbiF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v0rl2szuvpl9hwqbsnqa.png" alt="Custom Partial" width="880" height="209"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, what is going on in the example above? The first and foremost thing to understand is that by using mapped types we just simply iterating all properties of type &lt;code&gt;T&lt;/code&gt; that is passed as a generic argument to our custom partial type. But iterating is not just enough since partial means that all the properties should be optional. To do so, we use mapping modifiers, more specifically ?, which serves as optionality modifier.&lt;/p&gt;

&lt;h2&gt;
  
  
  Required
&lt;/h2&gt;

&lt;p&gt;The next utility type we'll take a look at today is &lt;code&gt;Required&amp;lt;T&amp;gt;&lt;/code&gt;. In general, it is simply opposite to &lt;code&gt;Partial&amp;lt;T&amp;gt;&lt;/code&gt; that accepts as generic argument some object of type &lt;code&gt;T&lt;/code&gt; and makes all of its properties required. To remove optionality modifier we simply prefix it with -.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eEg3_aWb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lm4hyqrqvo5bzh9jbg8j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eEg3_aWb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lm4hyqrqvo5bzh9jbg8j.png" alt="Custom Required" width="880" height="209"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;&lt;code&gt;Readonly&amp;lt;T&amp;gt;&lt;/code&gt; is yet another utility type that is immensely easy to implement. In this case, everything that is needed to make all properties of &lt;code&gt;T&lt;/code&gt; immutable is to add readonly mapping modifier.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aeQwC9Fp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lvqus5qkiu54o9rq0puf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aeQwC9Fp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lvqus5qkiu54o9rq0puf.png" alt="Custom Readonly" width="880" height="209"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Record
&lt;/h2&gt;

&lt;p&gt;So far, we've been talking about simple types that cause no problem to implement. On contrary, &lt;code&gt;Record&amp;lt;K, T&amp;gt;&lt;/code&gt; is a little bit different and is designed to construct an object of specific structure - to accept only certain types of values as its property keys and property values. The custom implementation of &lt;code&gt;Record&amp;lt;K, T&amp;gt;&lt;/code&gt; would look something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--s38Ns3q2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y6zz7kzle15alcrcc0pl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--s38Ns3q2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y6zz7kzle15alcrcc0pl.png" alt="Custom Record" width="880" height="209"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since the user is responsible for providing us with valid types that should be keys (K) instead of iterating all keys in &lt;code&gt;T&lt;/code&gt; as we did in &lt;code&gt;Partial&amp;lt;T&amp;gt;&lt;/code&gt;, &lt;code&gt;Required&amp;lt;T&amp;gt;&lt;/code&gt; and &lt;code&gt;Readonly&amp;lt;T&amp;gt;&lt;/code&gt; we simply iterate through all properties in &lt;code&gt;K&lt;/code&gt; - &lt;code&gt;P in K&lt;/code&gt;. Another thing worth our attention for better understanding is &lt;code&gt;K extends number | string | symbol&lt;/code&gt;. Since our custom record type is an object and as we know javascript object accepts as key values only string, number or symbol by typing &lt;code&gt;K extends number | string | symbol&lt;/code&gt; we just manually telling that our &lt;code&gt;MyRecord&amp;lt;K, T&amp;gt;&lt;/code&gt; should follow this rule as well.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final words
&lt;/h2&gt;

&lt;p&gt;Thanks for reading the first part of the "Deep dive into TypeScript Utility Types" series. Hope it will throw light on TypeScript Utility types and help to understand how they are implemented. Next time, we will discuss other types as &lt;code&gt;Pick&amp;lt;Type, Keys&amp;gt;&lt;/code&gt;, &lt;code&gt;Omit&amp;lt;Type, Keys&amp;gt;&lt;/code&gt;, &lt;code&gt;ReturnType&amp;lt;Type&amp;gt;&lt;/code&gt;, and much more.&lt;/p&gt;

&lt;p&gt;Keep in touch and happy coding.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>javascript</category>
      <category>programming</category>
    </item>
    <item>
      <title>Why I moved to TypeScript and have no regrets</title>
      <dc:creator>Vladyslav</dc:creator>
      <pubDate>Wed, 15 Jul 2020 13:45:09 +0000</pubDate>
      <link>https://dev.to/vbrdnk/why-i-moved-to-typescript-and-have-no-regrets-3174</link>
      <guid>https://dev.to/vbrdnk/why-i-moved-to-typescript-and-have-no-regrets-3174</guid>
      <description>&lt;p&gt;It‘s worth being said that it wasn’t my intention to move to TypeScript instead of vanilla JS. It just happened that we decided to move our production-ready project to Angular 4, which, as all of you know, uses TypeScript as a default language. Despite the fact that Angular projects can also be written in plain JavaScript, it’s documentation, examples, and most of the answers at StackOverflow are written in TypeScript. So, the decision was made and I have no single regret of doing that. Let me explain why.&lt;br&gt;
TypeScript provides us with a bunch of advantages that are not implemented in ECMAScript aka JavaScript yet. So, let’s discuss them one by one.&lt;/p&gt;
&lt;h3&gt;
  
  
  Types
&lt;/h3&gt;

&lt;p&gt;For those of you who didn’t work or hear about TypeScript:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;…it is a typed superset of JavaScript that compiles to plain JavaScript&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It becomes obvious from the language name that the main difference lies in the fact that TS uses types. So, yeah, it is strongly typed language, the feature that about 50% of JavaScript developers were missing and the remaining 50% hate in other strongly-typed languages. Without referring types of responses or other data, we had a lot of freedom in our code but at the same time this freedom is the main reason for lots of errors that are hardly debuggable (not sure if such a word even exists).&lt;br&gt;
From my perspective, types are great. They make your code more accurate and understandable for other developers. It makes much easier to see what method returns when you intentionality define its return type and it also prevents you from returning something else. The second benefit of typing is to see the response type of your Http request. We all know how painful it may be in JavaScript to debug errors when your code doesn’t work properly and the reason for that is that you’re referring to a response property which may not exist at all. Therefore, declaring response type prevents you, a developer, from this headache.&lt;/p&gt;
&lt;h3&gt;
  
  
  Classes and Interfaces
&lt;/h3&gt;

&lt;p&gt;For those of you who gonna tell me that ES6 introduced classes, my response would be “Yes, I know that”. However, typescript is not just about classes and types. If not mentioning that TS classes also provide us, developers, with public, private, static, readonly members of classes, it also introduces to us Abstract Classes and Interfaces, which we do not currently have in JavaScript.&lt;br&gt;
Abstract Classes are a part of more advanced topics of OOP rather than TypeScript but still, they are worth mentioning. Here’s a quote from the official TS documentation:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Abstract classes are base classes from which other classes may be derived. They may not be instantiated directly. Unlike an interface, an abstract class may contain implementation details for its members. The abstract keyword is used to define abstract classes as well as abstract methods within an abstract class.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The simplest and the most vivid example of Abstract Class is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;abstract class Animal { 
   abstract makeSound(): void; 
   move(): void { 
      console.log(“roaming the earth…”); 
   } 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Interfaces are another great feature that was introduced in TypeScript and which is actually used on a daily basis compared to Abstract Classes. Generally, in Typescript, interfaces fill the role of naming your own data types, and are considered a powerful way of defining contracts within your code as well as contracts with code outside of your project. It means that interface helps you define the skeleton of classes or data types, while their actual implementation may differ.&lt;/p&gt;

&lt;h3&gt;
  
  
  Public, Private, Protected, Static, Readonly Modifiers
&lt;/h3&gt;

&lt;p&gt;As it’s been mentioned before, ES6 introduced classes to JavaScript developers, but it missed one important thing — modifiers. Since TypeScript is a more advanced and progressive version of JavaScript, it successfully fills this gap. With such modifiers as public, private, protected, static, readonly, we are able to enclose variables, properties, and methods into the corresponding scope. Plus, it is no longer needed to use different workarounds like IIFE or revealing patterns. With TypeScript, you can easily define which fields and methods should be accessible outside classes and which should not, which ones we allow to change and which should always remain unchanged.&lt;/p&gt;

&lt;h3&gt;
  
  
  Namespaces and Modules
&lt;/h3&gt;

&lt;p&gt;Comparing to ES6, which has modules to help developers organize their code, TypeScript also gives them such a nice thing as namespaces that are heavily used in other OOP languages like C#.&lt;br&gt;
Briefly speaking, namespaces are a way of organizing code, dividing it into different parts, which gives us an access to the code from the other parts of an application.&lt;br&gt;
Using namespaces is especially useful when you do not want to spoil the global scope with a bunch of variables. Apart from that, interfaces are very handy when the application is quite big and we want to split the code across multiple files to make it easier to maintain in the future.&lt;/p&gt;

&lt;h3&gt;
  
  
  TypeScript Is Still JavaScript
&lt;/h3&gt;

&lt;p&gt;The last but not least thing I’d like to mention is that TypeScript is still JavaScript. TS is based on ES6 features and does not provide new methods or change the existing ones. If you compile your TS code into JavaScript, you will see the same Prototypical Inheritance. You will never see in a compiled JS code interfaces, decorators, modifiers — they just do not exist. Therefore, it is just a layer on top of JavaScript, which improves the experience of developers and prevents lots of unnecessary bugs from happening.&lt;br&gt;
On top of that, you are free to choose to which version of JavaScript your TypeScript code will be compiled, which means it will run even in Internet Explorer.&lt;br&gt;
Don’t take me wrong, I am not telling that we all should stop writing in JavaScript and move to TypeScript. JavaScript is a great language with an increasing pace of development. Its popularity is growing from year to year and now it is one of the most popular and frequently used programming languages. Still, there are a lot of areas for future improvements, and that’s where TypeScript comes into play.&lt;/p&gt;

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